Write a Python program to Convert String to Uppercase using the upper function, For Loop, while loop, and ASCII with an example.
Python Program to Convert String to Uppercase using upper() Function
This python program allows the user to enter a string. Next, we used a built-in string function called upper to convert lowercase characters in a string to uppercase.
TIP: Please refer String article to understand everything about Python Strings.
# Python Program to Convert String to Uppercase string = input("Please Enter your Own String : ") string1 = string.upper() print("\nOriginal String in Lowercase = ", string) print("The Given String in Uppercase = ", string1)
Python Convert String to Uppercase using For Loop
This python program allows the user to enter a string. Next, it finds lowercase letters and converts them to uppercase.
First, we used For Loop to iterate characters in a String. Inside the For Loop, we are using If Else Statement to check whether the character is between a and z or not. If true, we are subtracting 32 from its ASCII value. Otherwise, we are coping that character to string 1.
TIP: Please refer ASCII Value of Total Characters article and ASCII table to understand the ASCII values.
# Python Program to Convert String to Uppercase string = input("Please Enter your Own 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)
Python Program to Change String to Uppercase using While Loop
This python lowercase to uppercase conversion program is the same as above. However, we just replaced the For Loop with While Loop.
# Python Program to Convert String to Uppercase string = input("Please Enter your Own 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)
Python Program to Convert Lowercase String to Uppercase Example 4
This python string uppercase program is the same as the second example. However, we are using For Loop with Object
# Python Program to Convert String to Uppercase string = input("Please Enter your Own 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)
Python Program to Convert Uppercase using ASCII Values
In this program, we are comparing the ASCII values to check whether there are any lowercase characters in this string. If true, we are converting them to uppercase.
# Python Program to Convert String to Uppercase string = input("Please Enter your Own 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)