Saturday, September 21, 2024
Google search engine
HomeLanguagesPython String upper()

Python String upper()

Python String upper() method converts all lowercase characters in a string into uppercase characters and returns it. In this article, we’ll explore Python’s upper() method in-depth.

Python String upper() Syntax

Syntax: string.upper()

Parameters: The upper() method doesn’t take any parameters. 

Returns: returns an uppercased string of the given string.

String upper() in Python Example

Here we are using the string upper() in Python.

Python3




original_text = "Lazyroar for Lazyroar"
uppercase_text = original_text.upper()
 
print(uppercase_text)


Output

GEEKS FOR GEEKS

String with Case-Insensitive Comparison

In this example, we will take GFG as a user input to check for Python String and apply the string upper() function to check for case-sensitive comparison.

Python3




user_input = input("Enter your choice: ")
 
# Convert the user input to uppercase using the upper() method
uppercase_input = user_input.upper()
 
# Perform a case-insensitive comparison
if uppercase_input == "GFG":
    print("You chose 'GFG'.")
else:
    print("You didn't choose 'GFG'.")


Output

Enter your choice: No 
You didn't choose 'GFG'.

String with only Alphabetic Characters

In this example, we will take only alphabet characters, both lower and upper case characters as a Python String and apply the string upper() function to it.

Python3




text = 'geeKs For geEkS'
 
print("Original String:")
print(text)
 
# upper() function to convert
# string to upper_case
print("\nConverted String:")
print(text.upper())


Output

Original String:
geeKs For geEkS
Converted String:
GEEKS FOR GEEKS

String with Alphanumeric Characters 

In this example, we will take alphanumeric characters, i.e., a mixture of alphabets and numeric values, and then apply the string upper() function in Python.

Python3




text = 'g3Ek5 f0r gE3K5'
 
print("Original String:")
print(text)
 
# upper() function to convert
# string to upper_case
print("\nConverted String:")
print(text.upper())


Output

Original String:
g3Ek5 f0r gE3K5
Converted String:
G3EK5 F0R GE3K5

Use of upper() methods used in a Python Program

One of the common applications of the upper() method is to check if the two strings are the same or not. We will take two strings with different cases, apply upper() to them, and then check if they are the same or not.

Python3




text1 = 'Lazyroar fOr Lazyroar'
 
text2 = 'gEeKS fOR GeeKs'
 
# Comparison of strings using
# upper() method
if(text1.upper() == text2.upper()):
    print("Strings are same")
else:
    print("Strings are not same")


Output

Strings are same

RELATED ARTICLES

Most Popular

Recent Comments