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
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:
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
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 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())
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.
# 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())
The Original String: F1exip1e <span id="section4"></span> 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.
visitor_name001 = "Flexiple" print(visitor_name001.isupper()) visitor_name002 = "FLEXIPLE" print(visitor_name002.isupper())
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.
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.