In Python, we have a
upper()
method for string data values, which can convert all the lowercase letters to uppercase letters and return them. This method also has a sibling
isupper()
method that can check if a string contains only upper case characters or not.
When dealing with string data in Python, we encounter many problems where we need to convert all characters to uppercase letters. For example, we are creating a Python program that accepts a username from the user and converts it all into uppercase so it becomes more readable. To perform such a task, we can take the help of Python
upper()
and
isupper()
methods. The
isupper()
method is optional still, we can use it as a first check if the user has entered any lowercase in the username.
In this Python tutorial, we will discuss these two methods in detail and learn how to use them. We will also walk through the example solution for the above example statement to get a better idea of how to use these two methods in a Python program. So let's get started with a brief introduction of Python strings .
Python String Overview
A Python string is a collection of characters that can declare with the help of single
'string
' , double
""string
or triple
'''string'''
inverted quotes. The Python strings are immutable data types, which means once we have defined a string value we can not alter the defined characters. And all the methods like upper(), lower(), replace() etc, does not change the actual string value instead they made the changes on the copy of the actual string and return it.
Example
original_string = "Hello World" # convert the string into uppercase new_string = original_string.upper() print("Original String: ", original_string) print("New String: ", new_string)
Output
Original String: Hello World New String: HELLO WORLD
Python Upper Method
Python string objects support an inbuilt
upper()
method, that can convert all the lowercase alphabets present in the string to uppercase alphabets. The upper() method does not accept any argument and it returns the string value after converting the letters into upper case. As Python strings are immutables, which means the upper() method does not convert the actual string values into uppercase, instead it copies the string values and convert them into uppercase letters and return them. With this, we get a new value of uppercase string characters and the original string value remain intact.
Syntax
string_name.upper()
Example
Let's create a Python program that accepts the username from the user and, converts the username into all uppercase letters.
# input the username username = input("Enter Your UserName: ") # convert the username string to uppercase username = username.upper() print("Hello!", username)
Output
Enter Your UserName: Sourav kumar Hello! SOURAV KUMAR
Python isupper() Method
Python strings also support
isupper()
method, that can check if all the alphabets present in the string are uppercase letters. We can use this method before converting a string value to uppercase.
Syntax
string_name.isupper()
The
isupper()
method returns a boolean value True or False after checking string value. It returns True if all the alphabets in the string are uppercase, even if a single character in the string is lowercase the method return False.
Example
Now let's create
a program
where we have a list of usernames and we only need to convert those usernames into uppercase that contain lowercase characters.
# list of usernames usernames = ['Aarush Datta', 'ANHA BOSE', 'ArJun SuRa', 'BhavAna Krishnamurthy'] for sno, username in enumerate(usernames): # if the username is not all uppercase if not username.isupper(): username = username.upper() print(f"User {sno+1}: ", username)
Output
User 1: AARUSH DATTA User 2: ANHA BOSE User 3: ARJUN SURA User 4: BHAVANA KRISHNAMURTHY
Conclusion
Working with Python strings and converting them into uppercase is very common, that's why python comes with an inbuilt function that can convert a Python string value to upper case. The Python upper() is a string method that can convert a string's all lowercase values to upper case. Python string also provides an
isupper()
method that can check if the string only contains uppercase letters or not. And in this Python tutorial, we learned how to use these two methods.
People are also reading:
Leave a Comment on this Post