The Wayback Machine - https://web.archive.org/web/20250126094914/https://www.geeksforgeeks.org/python-string-upper/
Open In App

Python String upper() Method

Last Updated : 07 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

upper() method in Python is a built-in string method that converts all lowercase letters in a string to uppercase. This method is simple to use and does not modify the original string; instead, it returns a new string with the modifications applied.

Example:

Python
s = "learn python with gfg!"
res = s.upper()
print(res) 

Output
LEARN PYTHON WITH GFG!

Explanation:

  • The upper() method converts all the lowercase letters to uppercase and returns a new string.
  • The original string ‘s’ remains unchanged.

Syntax of upper() method

string.upper()

Parameters

  • The upper() method does not take any parameters.

Return Type

  • This method returns a new string in which all lowercase characters in the original string are converted to uppercase. If the original string has no lowercase letters then it returns the string unchanged.

Example of upper() method

1. Standardizing case for comparison

Let’s consider a scenario where we want to compare two strings without considering their case. The upper() method can help us standardize both strings to uppercase before comparison.

Python
s1 = "Python"
s2 = "PYTHON"

# Using upper() for comparison
res = s1.upper() == s2.upper()
print(res) 

Output
True

Explanation

  • The upper() method converts both s1 and s2 to uppercase.
  • The comparison s1.upper() == s2.upper() evaluates to True as both strings become “PYTHON”.

2. Handling mixed-case strings

upper() method is also very useful when we have a string with a mix of lowercase and uppercase letters, and we want to convert all characters to uppercase.

Python
# Converting mixed-case string to uppercase
s = "PyThoN is FuN"

res = s.upper()
print(res) 

Output
PYTHON IS FUN

Explanation

  • The upper() method processes the string ‘s’ and converts all lowercase letters to their uppercase equivalents.
  • Non-alphabetic characters like spaces remain unchanged.

Frequently Asked Question on upper() Method

Q1. What does upper() method return?

The upper() method returns a new string with all lowercase letters converted to uppercase and leaving other characters unchanged.

Q2. Does the upper() method modify the original string?

No, upper() method does not modify the original string but it returns a new string instead.

Q3. Can the upper() method convert non-alphabetic characters?

No, the upper() method only affects alphabetic characters. Non-alphabetic characters such as digits, spaces, and symbols remain unchanged.

Q4. Is the upper() method locale-sensitive?

No, the upper() method is not locale-sensitive. For locale-specific behavior, consider using the locale module.

Q5. Can the upper() method handle empty strings?

Yes, upper() method can handle empty strings and will return an empty string if called on one.


Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg