Skip to content

Python String upper() Method

How to use Python's string upper() method to convert all characters in a string to uppercase and handle case-insensitive string comparisons."

The String upper() method returns a new string with all characters converted to uppercase.

Syntax

my_string.upper()

upper() Parameters

Python String upper() method does not take any parameters.

upper() Return Value

Python String upper() method returns the uppercase string from the given string.

It converts all lowercase characters to uppercase. If no lowercase characters exist, it returns a copy of the original string.

Examples

Example 1:

The upper() method returns a new string with all characters converted to uppercase.

my_str = 'Tutorial Reference'
result = my_str.upper()
print(result) # Output: TUTORIAL REFERENCE

output

TUTORIAL REFERENCE

Example 2: Uppercase a String with Symbols with upper()

The upper() method ignores numbers and special characters in a string.

my_str = '1234 abc $@%*'
result = my_str.upper()
print(result) # Output: 1234 ABC $@%*

output

1234 ABC $@%*

Example 3: How to use upper() in a program

For example, let's check if two strings are the same, converting them to uppercase and then checking if they are equals:

str1 = "Tutorial Reference!"
str2 = "TuTORial REFereNCE!"

if(str1.upper() == str2.upper()):
    print("The strings are same.")
else:
    print("The strings are not same.")

output

The strings are same.
Navigation