Python String upper() Method



The Python string upper() method is used to convert all the lowercase characters present in a string into uppercase. However, if there are elements in the string that are already uppercased, the method will skip through those elements.

The upper() method can be used in applications where case-sensitivity is not considered. It works cohesively with the lower() method; where the string is converted into lowercased letters.

Note − This method will not show affect any non-casebased characters like digits and symbols.

Syntax

Following is the syntax for Python String upper() method −

str.upper()

Parameters

This method does not accept any parameters.

Return Value

This method returns a copy of the string in which all case-based characters have been uppercased.

Example

If the given string contains all lowercased letters, the method will return the string with all uppercased letters.

The following example shows the usage of Python String upper() method. We will create a string containing all lowercase letters, say "this is string example". The upper() method is called on this string and the return value obtained will the uppercased string of the input.

str = "this is string example";
print(str.upper())

When we run above program, it produces following result −

THIS IS STRING EXAMPLE

Example

If the given string contains all uppercased letters, the method will return the original string.

In this example, we will create a string containing all uppercase letters, "THIS IS STRING EXAMPLE". The upper() method is called on this string and the return value will be the original string.

str = "THIS IS STRING EXAMPLE";
print(str.upper())

When we run above program, it produces following result −

THIS IS STRING EXAMPLE

Example

Suppose the given string contains digits or symbols, the upper() method will not throw an error but returns the original string.

In the following example, a string "This is a digit/symbol string: 781261&*(*&&", containing digits and symbols is created. The upper() method is invoked on this input string as follows −

str = "This is a digit/symbol string: 781261&*(*&&";
print(str.upper())

On executing the above program, the output will be displayed as given below −

THIS IS A DIGIT/SYMBOL STRING: 781261&*(*&&

Example

In a non case sensitive environment, two strings are compared to see if they are equal using the upper() method. The return value will be either true or false.

In this example program, we take two input strings, "string example", "STRING example". Then, we call the upper() method on both these strings. Using the conditional statements, we check if both strings are equal after they are uppercased. The result is printed for either case.

str1 = "string example"
str2 = "STRING example"
if(str1.lower() == str2.lower()):
   print("Both strings are equal")
else:
   print("Both strings are not equal")

The output for the program above is displayed as −

Both strings are equal
python_strings.htm
Advertisements