Write a Python program to Convert String to Lowercase using the lower function, For Loop, while loop, and ASCII with an example.
Python Program to Convert String to Lowercase using lower Function
This python program allows the user to enter a string. Next, we used a built-in string function called lower to convert upper case characters in a string to lowercase.
TIP: Please refer String article to understand everything about it in Python.
string = input("Please Enter your Own String : ") string1 = string.lower() print("\nOriginal = ", string) print("Result = ", string1)
output
Please Enter your Own String : HELLO WORLD
Original = HELLO WORLD
Result = hello world
Python Program to Convert String to Lowercase using For Loop
This python program allows the user to enter a string. Next, it finds the uppercase letters and converts them to lowercase.
First, we used For Loop to iterate characters in a String. Inside the For Loop, we used the If Else Statement to check whether the character is between A and Z or not. If true, we are adding 32 to 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.
string = input("Please Enter your Own Text : ") 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) print("Result = ", string1)
Please Enter your Own Text : WELCOME TO TUtoriaL GATEWAY
Original = WELCOME TO TUtoriaL GATEWAY
Result = welcome to tutorial gateway
Program to Convert String to Lowercase using While Loop
This string uppercase to lowercase conversion program is the same as above. However, we just replaced the For Loop with While Loop.
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) print("After = ", string1)
Please Enter your Own Text : PYTHON TUTORIAL
Original = PYTHON TUTORIAL
After = python tutorial
Convert Uppercase String to Lowercase Example 4
This code to change uppercase to lowercase is the same as the second example. However, we are using For Loop with Object.
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) print("After = ", string1)
Convert String to Lowercase using ASCII Values
In this program, we are comparing the ASCII values to check if there are any uppercase characters. If true, we are converting them to lowercase.
str1 = input("Please Enter your Own Text : ") str2 = '' for i in str1: if(ord(i) >= 65 and ord(i) <= 90): str21 = str2 + chr((ord(i) + 32)) else: str2 = str2 + i print("\nOriginal = ", str1) print("Result = ", str21)
Please Enter your Own Text : TUTORIAL GATEWAY
Original = TUTORIAL GATEWAY
Result = tutorial gateway