upper() method converts all lowercase letters in a string to uppercase and returns the modified string. The Python
isupper() returns true if all of the characters in a string are uppercase, and false if they aren’t. Both are useful for formatting data that is dependant on case.
In Python, when you’re working a string, you may want to convert the string to uppercase, or check if the string is already in uppercase.
For example, if you’re creating a program that prepares the data for ticket stubs at a concert, you may want to convert all names to uppercase so that they are easily readable. Before you convert the string to uppercase, you may also want to check if it is already in uppercase.
That’s where the Python
isupper() methods come in. The
upper() method can be used to convert all case-based characters in a string to uppercase, and the
isupper() method can be used to check if all characters in a string are in uppercase.
We are going to discuss how to use both these methods to work with uppercase strings in this tutorial. We’ll also go through a few examples to illustrate how they work. If you’re interested in learning about lowercase string methods in Python, check out our guide to Python lowercase methods.
Python String Refresher
Strings store text and are typically held in a variable. Strings in Python are declared with single quotes
‘’ or double quotes
“”. While both single quotes and double quotes can be used, you should stick to one or the other. Here’s an example of how to declare a string:
string_name = "This is a Python string."
We can print a string with the
string_name = "This is a Python string." print(string_name)
Our program returns:
This is a Python string.
Now that we’ve brushed up on strings, let’s consider how we can convert them to uppercase.
The built-in Python
upper() method can be used to convert all case-based characters within a string to uppercase. The
upper() method returns a copy of an original string in which all characters appear in uppercase.
The syntax for the
upper() method is as follows:
As you can see, the
upper() method takes no parameters, and is appended to the end of an existing string value.
Let’s walk through an example to show how the
upper() method can be used.
Say that we are working at a movie theater and we are creating a program that converts all customer names to uppercase. We are doing this so that customer names are easily readable on tickets and use a consistent case, which will make it easier for the movie clerks to check the identity of moviegoers.
To accomplish this task, we could use the
upper() method. Here’s an example of the
upper() method being used to convert a string to uppercase:
attendee_name = "Elsie Swanson" print(attendee_name.upper())
Our code returns:
ELSIE SWANSON. Let’s break down our code to show how it works. On the first line, we declare a variable called
attendee_name that stores the name of our moviegoer. Then, on the next line, we use
upper() to convert
attendee_name to uppercase, and then we print the revised name to the console.
upper() method will not affect symbols, whitespaces, or numbers within a string, because those characters are not case-based.
Before you convert a string to uppercase, you may first want to check that the string is not already in uppercase form.
To check if a string is in uppercase, we can use the
isupper() checks whether every case-based character in a string is in uppercase, and returns a True or False value depending on the outcome.
Here’s the syntax for the
For instance, let’s use the movie ticket example from above.
Before we convert a moviegoer’s name to uppercase on their ticket, we should first check to see if the name has already been converted to uppercase. This would allow us to make our code more efficient because we would not be converting a string to uppercase that was already formatted in the correct way.
We can do check whether a customer’s name is already in uppercase by using the following code:
attendee_name = "Elsie Swanson" print(attendee_name.isupper())
Our code returns: False. As you can see, the value of the
attendee_name variable is in sentence case. So, when our
isupper() method is executed, it returns False, because not every character in the
attendee_name variable is in uppercase.
isupper() method returns True if a string includes whitespaces, symbols, or numbers, assuming all case-based characters appear in uppercase.
Python Isupper and Upper
Let’s go a step further with our code. In the above example, we used the
isupper() built-in function to check if the customer’s name was already in uppercase.
We could combine both
upper() to check if a string is already in uppercase, and convert it to uppercase if the string is not already formatted in all-uppercase. Here’s the code we could use to perform this action:
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
attendee_name = "Elsie Swanson" if attendee_name.isupper() == False: attendee_name = attendee_name.upper() print(attendee_name) else: print("This attendee's name is already in uppercase.") print(attendee_name)
The result of our code is as follows:
attendee_name was already in all-uppercase, our code would return:
This attendee's name is already in uppercase. ELSIE SWANSON
Let’s break down our code. On the first line, we declare the
attendee_name variable which stores the name of our moviegoer. Then, we use an
if statement and
isupper() to check whether or not the customer’s name is already in uppercase.
If the statement evaluates to False — which means our customer’s name is not in all-uppercase — our program uses the
upper() method to convert the customer’s name to uppercase and prints out the revised name to the console.
If the customer’s name is already in upper case, the contents of our
else statement are executed, and a message is printed to the console, alongside the name of the attendee.
Working with uppercase strings is a common string operation in Python. The
upper() method can be used to convert a string to all-uppercase, and the
isupper() method can be used to check whether a string is already in uppercase.
This tutorial discussed how to use both the
isupper() methods to work with uppercase strings in Python. We also explored an example of each of these methods in action, then we discussed how both of these methods can be combined to check if a string is in uppercase, and convert it to uppercase if it is not already in that form.
That’s all you need to know about the
isupper() methods — now you’re ready to use them in your code like a pro in your Python programs!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.