Python program to convert a string into uppercase

Python has an in-built function upper() that can be used to convert a string into uppercase. The upper() method extracts an uppercase string from a string input. All lowercase characters are changed to uppercase. If there are no lowercase characters, the original string is returned. The upper function doesn’t take any parameters.

Following representation of string conversion into uppercase −

  • cartoon ----------- CARTOON

  • GamE ----------- GAME

  • Play ------------- PLAY



The ord() is a predefined function used in Python to specify the unique code of a character.


This is a built-in function in Python that can be used to convert the string into uppercase.


The following steps are −

  • We are storing the string input to the variable named ‘str_name’.

  • Then use the upper method() in the given string to convert it into uppercase and store it in the variable ‘up_str_name’. Next, get the result using this variable.[Example 1]

  • Then store the empty value in the variable named ‘up_str_name’ which will later get the result of the string in uppercase. [Example 2]

  • Now start using for loop to iterate the character of a given string.

  • Then use the variable character ‘ch’ in a predefined method i.e. ord(ch) to store in the variable named ‘asc’ which represents the unique character code of ASCII.

  • Moving ahead to use an if-else statement to check the condition of an ASCII value of lowercase.

“asc > 96 and asc < 123”

    If any character is found between 97-122 it represents an alphabetical character (a-z).

  • There are two following conditions to understand the if-else statement −

  • a. up_str_name = up_str_name + chr(asc-32) − As we know the variable up_str_name is empty and by subtracting the character ascii with 32 it will get the uppercase character of a string.

    b. up_str_name = str_name + chr(asc) − Here we simply add a string and ASCII value of char into it which will not return the uppercase of a string.

  • Finally, we get the uppercase string value with the help of the variable named ‘up_str_name’.

Example 1

In this program, we will store the string value to the variable named ‘Str_name’ which will be used to change the string into uppercase. Then initialize the variable named ‘up_str_name’ and store the value by conversing the string in uppercase i.e. Str_name.upper(). Finally, we are getting value with the help of the variable ‘up_str_name’.

Str_name = "tUTorIx"
up_str_name = Str_name.upper()
print("The uppercase string is", up_str_name)


The uppercase string is:  TUTORIX

Example 2

In this example, we are going to convert a string into uppercase by using ASCII values.

str_name = "homework"
up_str_name =" "
for ch in str_name:
   asc = ord(ch)
   if asc > 96 and asc < 123:
      up_str_name = up_str_name + chr(asc-32)
      up_str_name = str_name + chr(asc)
print("The lowercase string is: ",up_str_name)


The uppercase string is: HOMEWORK


We explored the difference between examples 1 and 2 by converting a string into uppercase. In example 1, we simply solve the problem by using a predefined method. In the case of example 2, we use the concept of ASCII value and set the predefined method ord() to get the code of a unique character.

Updated on: 01-Jun-2023


Kickstart Your Career

Get certified by completing the course

Get Started