Python String upper() Function
The String upper()
method returns a new string with all characters converted to uppercase.
This method does not change the original string.
If you want to convert to lowercase string, use lower()
method. You can also use swapcase()
to swap between lowercase to uppercase.
Syntax
my_string.upper()
upper() Parameters
Python String upper()
function does not take any parameters.
upper() Return Value
Python String upper()
function 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.