Python String upper() Function
Last Updated :
20 Dec, 2023
Python String upper() method converts all lowercase characters in a string into uppercase characters and returns it.
Example:
Python3
original_text = "lisT Upper"
upper_text = original_text.upper()
print (upper_text)
|
What is the String upper() Method?
String upper() is an in-built function in Python, that converts all the letters in a string to uppercase(capital) and then returns it.
It is very useful for standardizing string cases, like when comparing case-insensitive strings.
Python String upper() Syntax
string.upper()
Parameters
- The upper() method doesn’t take any parameters.
Returns
returns an uppercase string of the given string.
How to Use String upper() Function?
The string upper() function is a simple and easy-to-use function. You just need to call the upper() function with the string object. Let’s understand how to convert string to uppercase(capital) with an example:
Python3
original_text = "convert to uppercase"
upper_text = original_text.upper()
print (upper_text)
|
Output
CONVERT TO UPPERCASE
Methods to Convert String to Uppercase
There are various ways How to Convert a String to Uppercase in Python, here we are discussing some generally used method for converting a string to uppercase in Python those are as follow.
1. Convert a String to Uppercase Using upper() Method
Here we are using the string upper() in Python.
In this example, the below code converts the string “geeks for geeks” to uppercase using the `upper()` method, and then prints the result: “GEEKS FOR GEEKS”.
Python3
original_text = "geeks for geeks"
uppercase_text = original_text.upper()
print (uppercase_text)
|
Output
GEEKS FOR GEEKS
2. Convert a String to Uppercase Using capitalize()
Method
The `capitalize()` method in Python converts the first character of a string to uppercase and the rest to lowercase, returning the modified string.
Example,: In this example the below code capitalizes the first letter of the string “geeks for geeks” and prints the modified string: “Geeks for geeks”.
Python3
original_text = "geeks for geeks"
capitalized_text = original_text.capitalize()
print (capitalized_text)
|
Output
Geeks for geeks
3. Convert a String to Uppercase Using casefold()
Method
The `casefold()` method in Python converts a string to lowercase and is suitable for case-insensitive comparisons. It is more aggressive than `lower()` and handles a broader range of Unicode characters.
Example 1: In this example the below code converts the string “GeEkS FoR GeEkS” to lowercase using `casefold()` for case-insensitive handling and prints the result: “geeks for geeks.”
Python3
original_text = "GeEkS FoR GeEkS"
casefolded_text = original_text.casefold()
print (casefolded_text)
|
Output :
geeks for geeks
4. Uppercase with Case-Insensitive Comparison
This method converts a string to uppercase in Python while allowing case-insensitive comparison by using the `upper()` method for the uniform casing.
Example 1: 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: " )
if user_input = = "GFG" :
print ( "You chose 'GFG'." )
else :
print ( "You didn't choose 'GFG'." )
|
Output
Enter your choice: gfg
You didn't choose 'GFG'.
Example 2: 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. In this example the below code checks if two strings (`text1` and `text2`) are the same, ignoring case, and prints the result.
Python3
text1 = 'geeks fOr geeks'
text2 = 'gEeKS fOR GeeKs'
if (text1.upper() = = text2.upper()):
print ( "Strings are same" )
else :
print ( "Strings are not same" )
|
Output
Strings are same
In this article, we have covered the definition, syntax, and use of the upper() function in Python. We have also seen different variations in using the upper() function and other methods to capitalize a string in Python.
upper() function is a very useful function for case-insensitive string comparison operations.
Read Other String Methods
Also Read:
Please Login to comment...