;

How to Convert the String to Uppercase in Python


Tutorialsrack 26/04/2020 Python

In this article, we will learn how to convert the string to uppercase to python. There are many ways to convert the string to uppercase in Python. We are converting a string to uppercase by using some methods such as python built-in function - upper() function, by using ASCII value, By Using Loops, etc.

Here are some examples of converting a string to uppercase in python.

Example 1: Convert String to Uppercase using upper() Function

In this example, we will use the Python built-in function - upper() to convert the string to lowercase.

Example 1 - Convert String to Uppercase using upper() Function
# Convert String to Uppercase using upper() Function

# Take The Input from User
string = input("Enter The String: ")

string1 = string.upper()
 
print("\nOriginal String in Lowercase  =  ", string)
print("The Given String in Uppercase =  ", string1)
Output

Enter The String: Tutorials Rack is an online learning website

 

Original String in Lowercase  =   Tutorials Rack is an online learning website

The Given String in Uppercase =   TUTORIALS RACK IS AN ONLINE LEARNING WEBSITE

Example 2: Convert String to Uppercase Using For Loop

In this example, we will use for loop and chr() and ord() function to convert the string to uppercase.

Example 2: Convert String to Uppercase Using For Loop

# Convert String to Uppercase Using For Loop

# Take the Input From the User
string = input("Enter The String: ")
string1 = ''

for i in range(len(string)):
    if(string[i] >= 'a' and string[i] <= 'z'):
        string1 = string1 + chr((ord(string[i]) - 32))
    else:
        string1 = string1 + string[i]
 
print("\nOriginal String in Lowercase  =  ", string)
print("The Given String in Uppercase =  ", string1)
Output

Enter The String: Tutorials Rack is an online learning website

 

Original String in Lowercase  =   Tutorials Rack is an online learning website

The Given String in Uppercase =   TUTORIALS RACK IS AN ONLINE LEARNING WEBSITE

Example 3: Convert String to Uppercase Using While Loop

In this example, we will use while loop, chr() and ord() function to convert the string to uppercase.

Example 3: Convert String to Uppercase Using While Loop

# Convert String to Uppercase Using While Loop

# Take the Input from User
string = input("Enter The String: ")
string1 = ''
i = 0

while(i < len(string)):
    if(string[i] >= 'a' and string[i] <= 'z'):
        string1 = string1 + chr((ord(string[i]) - 32))
    else:
        string1 = string1 + string[i]
    i = i + 1
 
print("\nOriginal String in Lowercase  =  ", string)
print("The Given String in Uppercase =  ", string1)
Output

Enter The String: Tutorials Rack is an online learning website

 

Original String in Lowercase  =   Tutorials Rack is an online learning website

The Given String in Uppercase =   TUTORIALS RACK IS AN ONLINE LEARNING WEBSITE

Example 4: Convert String to Uppercase using ASCII Values

In this example, we will use ASCII values and chr() and ord() functions to convert the string to uppercase

Example 4: Convert String to Uppercase using ASCII Values

# Convert String to Uppercase using ASCII Values

# Take the Input from the User
string = input("Enter The String: ")
string1 = ''

for i in string:
    if(ord(i) >= 97 and ord(i) <= 122):
        string1 = string1 + chr((ord(i) - 32))
    else:
        string1 = string1 + i
 
print("\nOriginal String in Lowercase  =  ", string)
print("The Given String in Uppercase =  ", string1)
Output

Enter The String: Tutorials Rack is an online learning website

 

Original String in Lowercase  =   Tutorials Rack is an online learning website

The Given String in Uppercase =   TUTORIALS RACK IS AN ONLINE LEARNING WEBSITE

Example 5: Convert Lowercase String to Uppercase

Example 5: Convert Lowercase String to Uppercase

# Convert Lowercase String to Uppercase

string = input("Enter The String: ")
string1 = ''

for i in string:
    if(i >= 'a' and i <= 'z'):
        string1 = string1 + chr((ord(i) - 32))
    else:
        string1 = string1 + i
 
print("\nOriginal String in Lowercase  =  ", string)
print("The Given String in Uppercase =  ", string1)
Output

Enter The String: Tutorials Rack is an online learning website

 

Original String in Lowercase  =   Tutorials Rack is an online learning website

The Given String in Uppercase =   TUTORIALS RACK IS AN ONLINE LEARNING WEBSITE

I hope this article will help you to understand how to convert the string to uppercase in python.

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags