Python uppercase string

The upper() function translates all the lowercase characters in a string into uppercase and returns the string. The upper() function is an integral function in Python. In certain cases, the upper() function is very useful. For example, if we are developing a university management system and want to convert the name of all the students into uppercase letters, in this case, we will definitely use the upper() function. This article explains the use of the upper() function with the help of simple examples.

The syntax of upper() function

The syntax of the upper() function is:

str.upper()

While using the upper() function, we just have to write the name of our string and call the upper() function. Let’s see the examples of upper() function.

Examples

Let’s declare a lowercase character string and convert it to the uppercase characters.

#declaring a lowercase string
name = "kamran sattar awaisi"
#printing the original string
print("This is the original string:")
print(name)

#converting the string into uppercase
print("This is the converted string:")
print(name.upper())

Output

Now let’s declare a string that has few lowercase letters and few uppercase letters. The upper() function will convert the whole string into uppercase letters.

#declaring a lowercase string
name = "LinuxHint is WEb BasEd Learning PoRtal"
#printing the original string
print("This is the original string:")

print(name)

#converting the string into uppercase
print("This is the converted string:")
print(name.upper())

Output

There are many applications of the upper() function. For example, Python is a case sensitive language. If we want to compare the two string, we can convert them into uppercase, and then we can compare them.

#declaring first string
name_str1= "kamran sattar awaisi"
#declaring the second string
name_str2="KAMrAn saTTAR aWaiSI"
#converting the strings into uppercase and comparing them
if name_str1.upper()==name_str2.upper():
    print("Both the strings are same")
else:
    print("The strings are not same")

Output

Conclusion

The upper() function is used to convert the string uppercase characters. This article explains the use of the upper() function with the help of simple examples.



from Linux Hint https://ift.tt/35i1CN9

Post a Comment

0 Comments