Convert all characters of string to Uppercase in Python

This tutorial will help you to understand the different ways of converting all characters of a string to Uppercase in Python. We shall convert the string to Uppercase by using in-built functions in python as well as without using any in-built function with the help of a few examples. Also, you will understand the application of converting characters of a string to Uppercase with the help of a real-time example.

Converting all characters to Uppercase without using the in-built Function

# Converting string to Uppercase without using inbuilt function
text=input("Enter string: ")
for ch in text:
    if ch  in 'abcdefghijklmnopqrstuvwqxyz':
        a= ord(ch)
        b = a- 32
        output = output+chr(b)
print(f"The resultant UpperCase String is: {output}")


Enter string: hello From cOdeSpeedy!

The resultant UpperCase String is: HELLO FROM CODESPEEDY!

In the above code, we initialize two variables

  1. text: To store user input
  2. output: To store the resultant UpperCase string

Then we use a for loop and iterate over every character of the text string. We check if the character is present in the string that contains all lowercase characters from [a-z]. If the character is not present in the string, then we simply append the character to the output string. Else if the character is present in the string, it means that it is a lowercase character. We use the ord() function to convert the character into its Unicode value and subtract 32 from it to get the Unicode value of its corresponding Uppercase character. We then use the chr() function to convert the Unicode value back to its character format.

For example, if the character is ‘a’ then ord(‘a’)=97, b=97-32 =65 and chr(65)=’A’. Thus the character in lowercase can be successfully converted to Uppercase.

Converting all characters to Uppercase using the upper() function

Since converting all characters of a string to uppercase without using a function can be a tedious task, we can use an in-built function called upper() in python which takes a string as an input and returns the output string with all of its characters converted into the uppercase format.

Syntax of upper() function


Points to Remember:

  • The upper() function does not take any arguments and thus if you try to insert any parameters in the function then it gives an Exception.
  • The function converts only lowercase characters to uppercase and leaves digits and other special characters as it is.

Simple example of upper() function

#Converting string to Uppercase using upper() function
text=input("Enter String: ")
print(f"Original String: {text}")
print(f"Formatted string: {new_text}")


Enter String: how are you today?

Original String: how are you today?
Formatted string: HOW ARE YOU TODAY?

In the above code, we have taken a text string as an input from the user and used the upper() function to convert the string to the Uppercase version. Here, the string contains a question mark which is left as it is in the output string.

Application of upper() function

One of the applications of the upper() function is to check whether two strings that are formatted in different ways are actually the same or not.

#Application of upper() function
text1='wheRe Are YOU?'
text2='WHere ARE You?'
    print("Both strings are same")
    print("Both strings are not same")


Both strings are same

Real-time application of the upper() function

Problem statement:
Consider an eCommerce website that stores the login ID of the users in its database. It stores the login IDs only if the following conditions are satisfied:
The login ID must be stored in the uppercase version only and no two users should have the same login ID.

#Real time application of upper() in python
text=input("Enter user ID: ")
    if(text.upper() not in user_id):
        print("This user ID is aldready take. Please enter a different ID")
    text=input("Enter user ID: ")


Enter user ID: richard
Enter user ID: steven
Enter user ID: RicharD
This user ID is aldready take. Please enter a different ID
Enter user ID: alex

In the above code, three users are trying to register on the website. Login ID of the users is stored in the uppercase version using the upper() function. A list called user_id is used to store all the login IDs of the users. The code checks if the uppercase version of the Login ID is present in the list or not. If it is not present, then we simply append the ID to the list. If it is present in the list then we prompt the user to enter his ID again. Thus we make use of the upper() function to solve the given problem statement.

This is the end of the tutorial on how to convert all the characters of a string to Uppercase in Python.

Leave a Reply

Your email address will not be published. Required fields are marked *