Cheap Jerseys Cheap NFL Jerseys Cheap NFL Jerseys Cheap Football jerseys Cheap NFL Jerseys Wholesale Jerseys Cheap NHL Jerseys Cheap Jerseys From China Cheap MLB Jerseys Cheap Jerseys Cheap Jerseys Cheap Jerseys Wholesale Jerseys Python String upper() Method - WTMatter

Python String upper() Method

Python String Upper() Method

This is a tutorial of the Python String upper() method. Learn to covert a given Python String into uppercase using this method with the help of examples.

Python String upper()

The str.upper() method returns a string in which all of the characters of a given Python String are converted into their uppercase forms.

Note. The original string remains unchanged.

Similarly, you can also use the Python String lower() method to convert all of the characters in a given string into their lowercase forms. There’s also another method, Python String swapcase() to convert all of the lowercase characters of a given string into their uppercase forms and all of the uppercase characters into their lowercase forms.

Syntax

Given below is the syntax of the str.upper() method.

upper() Parameters

This method does not take any arguments as it only checks the given string on which it is applied, so no additional data is required in the form of arguments.

upper() Return Value

It returns the uppercase form of the given Python String on which this method is applied. In other words, all of the lowercase characters present in the original string will be converted into their uppercase forms in the returned string and if no character in the given string will be in uppercase, the original string will be returned as it is.

Examples

Given below are the two examples demonstrating the usage of the Python String upper() method.

Example 1. Converting a given Python String into uppercase using str.upper() method

In this example, we’re applying the method upper() on several different strings to get the uppercase strings for all of them.

#lowercase string
line = "this string is in lowercase"
print(line.upper())

#uppercase string
line = "THIS STRING IS IN UPPERCASE"
print(line.upper())

#alternate case
line = "ThIs sTrInG Is iN AlTeRnAtE CaSe"
print(line.upper())

#Title Case
line = "This String Is In Title Case"
print(line.upper())

#Alphanumeric String
line = "Hello12345!@#$%"
print(line.upper())

Output.

THIS STRING IS IN LOWERCASE
THIS STRING IS IN UPPERCASE
THIS STRING IS IN ALTERNATE CASE
THIS STRING IS IN TITLE CASE
HELLO12345!@#$%

Example 2. Typical usage of the str.upper() method in a Python Program

In the following example, we’ve first compared, if the two defined strings, string1 and string2 are equal or not as the condition for the first outer if-else statement.

This condition will be true if and only if the two strings are exactly the same, i.e. their contents as well as the case is the same. But in our case, the contents of the strings are same but their cases are different. Hence, the control of the program will be shifted to the else block.

Coming to the else block, one thing is clear, either the contents of the two strings are different or their cases. Hence, we printed it out to the console. Now, again, in another if-else statement, we’ve written a condition in which we’re comparing the uppercase forms of the two strings. As the Python String upper() method will return the uppercase forms of the strings, then the comparison of the contents of the two strings becomes clear. Now as the uppercase form of the string1 is equal to the uppercase form of the string2, hence, the print statement that is written inside the if block, will be executed.

string1 = "you are learning Python."
string2 = "YOU ARE LEARNING PYTHON."

if(string1 == string2):
    print("The contents and the case of the string1 and string2 is the same.")
else:
    print("The contents or the case of the string1 and string2 might not be the same.")
    if(string1.upper() == string2.upper()):
        print("The contents of the string1 and string2 are exactly the same but they are in different cases.")
    else:
        print("The contents as well as the cases of the two strings are not the same.")

Output.

The contents or the case of the string1 and string2 might not be the same.
The contents of the string1 and string2 are exactly the same but they are in different cases.

I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *