Open In App

Python String upper() Function

Last Updated : 20 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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)


Output

LIST UPPER

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




#initializing a string
original_text = "convert to uppercase"
#using upper function
upper_text = original_text.upper()
#printing uppercase string
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: ")
 
# Convert the user input to uppercase using the upper() method
 
# Perform a case-insensitive comparison
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'
 
# 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

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:



Previous Article
Next Article

Similar Reads

numpy string operations | upper() function
numpy.core.defchararray.upper(arr): function is used to return an array with the elements converted to uppercase. Parameters: arr : [ array_like ] Input array which may be str or unicode. Returns : [ndarray] Output uppercased array of str or unicode, depending on input type. Code #1: # Python Program explaining # numpy.char.upper() function import
1 min read
Python String Methods | Set 1 (find, rfind, startwith, endwith, islower, isupper, lower, upper, swapcase & title)
Some of the string basics have been covered in the below articles Strings Part-1 Strings Part-2 The important string methods will be discussed in this article1. find("string", beg, end) :- This function is used to find the position of the substring within a string.It takes 3 arguments, substring , starting index( by default 0) and ending index( by
4 min read
Python program to count upper and lower case characters without using inbuilt functions
Given a string that contains both upper and lower case characters in it. The task is to count a number of upper and lower case characters in it. Examples :Input : Introduction to Python Output : Lower Case characters : 18 Upper case characters : 2 Input : Welcome to GeeksforGeeks Output : Lower Case characters : 19 Upper case characters: 3Method 1:
3 min read
Python | Pandas Series.str.lower(), upper() and title()
Python is a great language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages, making importing and analyzing data much easier. Python has some inbuilt methods to convert a string into a lower, upper, or Camel case. But these methods don't work on lists and other multi-st
4 min read
Python regex to find sequences of one upper case letter followed by lower case letters
Write a Python Program to find sequences of one upper case letter followed by lower case letters. If found, print 'Yes', otherwise 'No'. Examples: Input : GeeksOutput : YesInput : geeksforgeeksOutput : NoPython regex to find sequences of one upper case letter followed by lower case lettersUsing re.search() To check if the sequence of one upper case
2 min read
Python - Extract Upper Case Characters
Sometimes, while working with strings, we are concerned about the case sensitivity of strings and might require getting just a specific case of character in a long string. Let’s discuss certain ways in which only uppercase letters can be extracted from a string. Method #1: Using list comprehension + isupper() List comprehension and isupper function
6 min read
Python Program to check if matrix is upper triangular
Given a square matrix and the task is to check the matrix is in upper triangular form or not. A square matrix is called upper triangular if all the entries below the main diagonal are zero. Examples: Input : mat[4][4] = {{1, 3, 5, 3}, {0, 4, 6, 2}, {0, 0, 2, 5}, {0, 0, 0, 6}}; Output : Matrix is in Upper Triangular form. Input : mat[4][4] = {{5, 6,
2 min read
PyQt5 QDial - Setting Upper Bound
In this article we will see how we can set the upper bound of QDial. Upper bound means the highest value QDial can handle by default the maximum value is 99 although we can change it any time. With the upper bound we can make sure the number above the upper bound can be assigned. Setting upper bound will not affect the movement of the Qdial it will
2 min read
PyQt5 QDial - Getting Upper Bound
In this article we will see how we can get the upper bound of QDial. Upper bound means the highest value QDial can handle by default the maximum value is 99 although we can change it any time with the help of setMaximum method. With the upper bound we can make sure the number above the upper bound can be assigned. Setting upper bound will not effec
2 min read
Pandas - Convert the first and last character of each word to upper case in a series
In python, if we wish to convert only the first character of every word to uppercase, we can use the capitalize() method. Or we can take just the first character of the string and change it to uppercase using the upper() method. So, to convert the first and last character of each word to upper case in a series we will be using a similar approach. F
2 min read
Practice Tags :