Python String upper() Method
Last Updated :
15 Nov, 2024
The upper() is a method of string objects in Python. It creates a new string with all lowercase letters changed to uppercase. This method does not change the original string instead it simply returns a new one.
Python
s = "hello, world!"
res = s.upper()
print(res)
Explanation: Here, all lowercase letters have been converted to uppercase.
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
Let’s take an example to see how upper() method works on a string that includes non-alphabetic characters and mixed cases.
Python
s = "hello123!@# WorlD"
res = s.upper()
print(res)
Explanation: Here, numbers and special characters remain unchanged while the letters are converted to uppercase.
Practical applications of upper()
The upper() method is very useful in many scenarios such as making case-insensitive comparisons between strings.
Python
s1 = "Hello"
s2 = "hello"
if s1.upper() == s2.upper():
print("The strings are equal.")
else:
print("The strings are not equal.")
OutputThe strings are equal.
Explanation: In this example, we convert both s1 and s2 to uppercase using upper() before comparing them. This approach ensures that the comparison is not affected by case differences.
Related Article:
Frequently Asked Question on upper() Method
What does upper() method return?
The upper() method returns a new string with all lowercase letters converted to uppercase and leaving other characters unchanged.
Does the upper() method modify the original string?
No, upper() method does not modify the original string but it returns a new string instead.
Can the upper() method handle empty strings?
Yes, upper() method can handle empty strings and will return an empty string if called on one.