Python Uppercase - All you need to know

In this short tutorial, we are going to look into what Python’s uppercase methods are. We will also consider some examples to clearly understand the concepts.

Table of Contents - Python Uppercase

  • An introduction to Python uppercase
  • Syntax and parameters
  • Python upper with examples
  • Python isupper with examples
  • Closing thoughts
  • Other Related Concepts

An introduction to Python uppercase

While working with strings, there might be instances where we need to convert lowercase strings to uppercase. Or we might have to check if a given string is uppercase or not. Python facilitates this with the following functions:
The Python upper() method is used to convert lowercase letters in a string to uppercase. The isupper() method, on the other hand, returns True if all the letters in a string are uppercase.

A real-life use-case of this function is an amusement park ticketing application. You may want to convert all the names of the visitors to uppercase format before printing them on the ticket. This helps with uniformity in storage and easy readability during the ticket check process.

Syntax and parameters

The syntax of the method upper() is:
string.upper()
As you can see, the method does not take any parameters. In the place of string we enter the name of the variable containing the string.


Python upper with examples

In this section, we learn about the Python upper() method. This method is used to convert lowercase string to uppercase in Python. It returns a modified string and not a newly created string.
In an instance where there are no lowercase letters, it returns the original string.

Input:

# Python program to show the working of the upper() function
visitor007 = 'Flexiple'
          
print("Original String:")
print(visitor007)
          
# upper() function converts string to uppercase
print("\nModified String:")
print(visitor007.upper())

Output:

Original String:
Flexiple
            
Modified String:
FLEXIPLE
In the above code snippet, we declare a variable called visitor007. This variable stores the name of our amusement park visitor. Secondly, we use Python’s upper() method to convert the visitor007 to uppercase. Finally, the revised name is printed.

The upper() method does not affect whitespace, numbers or symbols with a string. This is because those characters aren’t upper/lower case based. Let’s look at an example for the same.

Input:

# Python program for upper() function
visitor007 = 'F1exip1e'
          
print("The Original String:")
print(visitor007)
          
# upper() function converts string to uppercase
print("\nModified String:")
print(visitor007.upper())

Output:

The Original String:
F1exip1e
         
Modified String:
F1EXIP1E

Python isupper with examples

Before we do convert a string to uppercase, we might have to check if it is already in uppercase . The isupper() method checks every case-based character. If it finds any characters in lowercase it returns False, else it returns True.

Input:

visitor_name001 = "Flexiple"
print(visitor_name001.isupper())
        
visitor_name002 = "FLEXIPLE"
print(visitor_name002.isupper())

Output:

False
True
In the output of our first print statement, the method has returned False. This is because the function found lowercase characters in the string. Whereas, the second print statement has printed out True. This is because the string contains only uppercase letters.

Closing thoughts

Switching between the character cases is a common string operation in Python. In this Python tutorial, we looked at how the upper() method is used. We learned how the isupper() method is used to check a given string. We also looked into a couple of examples to understand both the methods completely.