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

Python String upper() Function

Last Updated : 20 Dec, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 | 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
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
Python string length | len() function to find string length
The string len() function returns the length of the string. In this article, we will see how to find the length of a string using the string len() method. Example: [GFGTABS] Python string = "Geeksforgeeks" print(len(string)) [/GFGTABS]Output13String len() Syntaxlen(string) ParameterString: string of which you want to find the length. Retu
5 min read
String slicing in Python to check if a string can become empty by recursive deletion
Given a string “str” and another string “sub_str”. We are allowed to delete “sub_str” from “str” any number of times. It is also given that the “sub_str” appears only once at a time. The task is to find if “str” can become empty by removing “sub_str” again and again. Examples: Input : str = "GEEGEEKSKS", sub_str = "GEEKS" Output : Yes Explanation :
2 min read
String slicing in Python to Rotate a String
Given a string of size n, write functions to perform following operations on string. Left (Or anticlockwise) rotate the given string by d elements (where d <= n).Right (Or clockwise) rotate the given string by d elements (where d <= n).Examples: Input : s = "GeeksforGeeks" d = 2Output : Left Rotation : "eksforGeeksGe" Right Rotation : "ksGeek
3 min read
Python | Sorting string using order defined by another string
Given two strings (of lowercase letters), a pattern and a string. The task is to sort string according to the order defined by pattern and return the reverse of it. It may be assumed that pattern has all characters of the string and all characters in pattern appear only once. Examples: Input : pat = "asbcklfdmegnot", str = "eksge" Output : str = "g
2 min read
String Alignment in Python f-string
Text Alignment in Python is useful for printing out clean formatted output. Some times the data to be printed varies in length which makes it look messy when printed. By using String Alignment the output string can be aligned by defining the alignment as left, right or center and also defining space (width) to reserve for the string. Approach : We
2 min read
String to Int and Int to String in Python
Python defines type conversion functions to directly convert one data type to another. This article is aimed at providing the information about converting a string to int and int to string. Converting a string to an int If we want to convert a number that is represented in the string to int, we have to use the int() function. This function is used
2 min read
Pad or fill a string by a variable in Python using f-string
f-string stands for formatted string. It had come up by Python Version 3.6 and rapidly used to do easy formatting on strings. F-string is a string literal having syntax starts with f and followed by {}. That placeholder used for holding variable, that will be changed upon the variable names and their values respectively. There are already strings f
4 min read
Convert Unicode String to a Byte String in Python
Python is a versatile programming language known for its simplicity and readability. Unicode support is a crucial aspect of Python, allowing developers to handle characters from various scripts and languages. However, there are instances where you might need to convert a Unicode string to a regular string. In this article, we will explore five diff
2 min read
Python String Formatting - How to format String?
String formatting allows you to create dynamic strings by combining variables and values. In this article, we will discuss about 5 ways to format a string. You will learn different methods of string formatting with examples for better understanding. Let's look at them now! How to Format Strings in PythonThere are five different ways to perform stri
10 min read
Maximum length of consecutive 1's in a binary string in Python using Map function
We are given a binary string containing 1's and 0's. Find the maximum length of consecutive 1's in it. Examples: Input : str = '11000111101010111' Output : 4 We have an existing solution for this problem please refer to Maximum consecutive one’s (or zeros) in a binary array link. We can solve this problem within single line of code in Python. The a
1 min read
Python | Permutation of a given string using inbuilt function
A permutation, also called an “arrangement number” or “order”, is a rearrangement of the elements of an ordered list S into a one-to-one correspondence with S itself. A string of length n has n! permutation. Examples: Input : str = 'ABC' Output : ABC ACB BAC BCA CAB CBA We have existing solution for this problem please refer Permutations of a given
2 min read
Python String - removeprefix() function
Python String removeprefix() function removes the prefix and returns the rest of the string. If the prefix string is not found, then it returns the original string. It is introduced in Python 3.9.0 version. Python String removeprefix() Method Syntax Syntax: str_obj_name.removeprefix(prefix Parameters: prefix- prefix string that we are checking for.
2 min read
Call a function by a String name - Python
In this article, we will see how to call a function of a module by using its name (a string) in Python. Basically, we use a function of any module as a string, let's say, we want to use randint() function of a random module, which takes 2 parameters [Start, End] and generates a random value between start(inclusive) and end(inclusive). Here, we will
3 min read
Wand function() function in Python
function() function is similar to evaluate function. In function() function pixel channels can be manipulated by applies a multi-argument function to pixel channels. Following are the list of FUNCTION_TYPES in Wand: 'undefined''arcsin''arctan''polynomial''sinusoid' Syntax : wand.image.function(function, arguments, channel) Parameters : ParameterInp
1 min read
Python - Call function from another function
Prerequisite: Functions in Python In Python, any written function can be called by another function. Note that this could be the most elegant way of breaking a problem into chunks of small problems. In this article, we will learn how can we call a defined function from another function with the help of multiple examples.  What is Calling a Function
5 min read
Returning a function from a function - Python
Functions in Python are first-class objects. First-class objects in a language are handled uniformly throughout. They may be stored in data structures, passed as arguments, or used in control structures. Properties of first-class functions: A function is an instance of the Object type.You can store the function in a variable.You can pass the functi
4 min read
Python math.sqrt() function | Find Square Root in Python
sqrt() function returns square root of any number. It is an inbuilt function in Python programming language. In this article, we will learn more about the Python Program to Find the Square Root. sqrt() Function We can calculate square root in Python using the sqrt() function from the math module. In this example, we are calculating the square root
3 min read
Lexicographically smallest string which is not a subsequence of given string
Given a string S, the task is to find the string which is lexicographically smallest and not a subsequence of the given string S. Examples: Input: S = "abcdefghijklmnopqrstuvwxyz"Output: aaExplanation:String "aa" is the lexicographically smallest string which is not present in the given string as a subsequence. Input: S = "aaaa"Output: aaabExplanat
5 min read
Sum of frequencies of characters of a string present in another string
Given two strings S1 and S2 of lengths M and N respectively, the task is to calculate the sum of the frequencies of the characters of string S1 in the string S2. Examples: Input: S1 = "pPKf", S2 = "KKKttsdppfP"Output: 7Explanation:The character 'p' occurs twice in the string S2.The character 'P' occurs once in the string S2.The character 'K' occurs
5 min read
Check if a string can be repeated to make another string
Given two strings a and b, the task is to check how many times the string a can be repeated to generate the string b. If b cannot be generated by repeating a then print -1. Examples: Input: a = "geeks", b = "geeksgeeks" Output: 2 "geeks" can be repeated twice to generate "geeksgeeks" Input: a = "df", b = "dfgrt" Output: -1 Recommended: Please try y
9 min read
numpy string operations | isupper() function
numpy.core.defchararray.isupper(arr) function returns True for each element if all cased characters in the string are uppercase and there is at least one character, it returns false otherwise. Parameters: arr : array_like of str or unicode Returns : [ndarray] Output array of bools. Code #1: # Python Program explaining # numpy.char.isupper() functio
1 min read
numpy string operations | istitle() function
numpy.core.defchararray.istitle(arr) function returns True for each element in the array if the element is a titlecased string and there is at least one character, it returns false otherwise. Parameters: arr : array_like of str or unicode Returns : [ndarray] Output array of bools. Code #1: # Python Program explaining # numpy.char.istitle() function
1 min read
numpy string operations | islower() function
numpy.core.defchararray.islower(arr) function returns True for each element if all cased characters in the string are lowercase and there is at least one cased character, It returns false otherwise. Parameters: arr : array_like of str or unicode Returns : [ndarray] Output array of bools. Code #1: # Python Program explaining # numpy.char.islower() f
1 min read
numpy string operations | lower() function
numpy.core.defchararray.lower(arr) function is used to return an array with the elements converted to lowercase. Parameters: arr : [ array_like ] Input array which may be str or unicode. Returns : [ndarray] Output lowercased array of str or unicode, depending on input type. Code #1: # Python Program explaining # numpy.char.lower() function import n
1 min read
Practice Tags :