Python String upper() Method

The upper() method converts all lowercase characters in a string to uppercase.

Syntax

string.upper()

Parameters

It doesn’t take any parameters

Return Value

It returns a new string with all lowercase letters converted to uppercase.

Examples

Basic Usage

text = "Hello"
print(text.upper()) # Output: HELLO

String with Numbers and Symbols

Characters already uppercase or not letters like numbers, symbols, and whitespace remain unchanged.

mixed_text = "I ran 5 miles this morning!"
print(mixed_text.upper()) # Output: I RAN 5 MILES THIS MORNING!

Practical Application

The upper() method is commonly used for case-insensitive comparisons and text formatting.

Case-insensitive Comparison

You can use upper() method to compare strings regardless of their case.

text1 = "Python"
text2 = "python"

if text1.upper() == text2.upper():
    print("They are equal")
else:
    print("They are not equal")

Output:

They are equal

Formatting Text

You can use upper() method to display user input or message in uppercase for emphasis. It is also essential when systems or data formats require uppercase text, such as for airport codes ("JFK", "LAX"), country codes ("US", "GB"), vehicle license plates, or product serial numbers.

user_input = input("Enter the vehicle license plate: ")

if user_input.upper() == "ABCD":
    print("You vehicle is fine")
else:
    print("Please repair your vehicle")

In this code example, we ask the user to enter a vehicle license plate. The input is then converted to uppercase using the upper() method before comparing it to the string "ABCD" (which represents a predefined license plate). This way, no matter how the user types "ABCD", whether it is "abcd", "Abcd", or "ABCD", we can handle it consistently.