Write a Python program to Toggle Characters Case in a String using swapcase, For Loop, while loop, and ASCII with an example.
Python Program to Toggle Characters Case in a String using swapcase() Function
This python program allows the user to enter a string. Next, we used swapcace string function to convert lowercase characters in a string to uppercase, and uppercase characters to lowercase.
# Python Program to Toggle Characters Case in a String
string = input("Please Enter your Own String : ")
string1 = string.swapcase()
print("\nOriginal String = ", string)
print("The Given String After Toggling Case = ", string1)
Python toggle string character cases output. Refer to the Python swapcase article.
Please Enter your Own String : Python PROgrams
Original String = Python PROgrams
The Given String After Toggling Case = pYTHON proGRAMS
Python Program to Toggle Characters Case in a String using For Loop
This python program allows the user to enter a string. Next, it toggles lowercase letters to uppercase and uppercase characters to lowercase.
First, we used Python For Loop to iterate characters in a Python String. Inside the Python For Loop, we are using the Python elif Statement.
- The first statement is to check whether the character is between a and z or not. If true, we are subtracting 32 from its ASCII value
- The second statement checks whether the character is between A and Z. If true, we are adding 32 to its ASCII value.
- Otherwise, we are coping that character to string 1.
TIP: Please refer Python program for ASCII Value of Total Characters article and ASCII table to understand the ASCII values.
# Python Program to Toggle Characters Case in a String
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))
elif(string[i] >= 'A' and string[i] <= 'Z'):
string1 = string1 + chr((ord(string[i]) + 32))
else:
string1 = string1 + string[i]
print("\nOriginal String = ", string)
print("The Given String After Toggling Case = ", string1)
Python toggle string character cases output
Please Enter your Own String : HellO WOrlD
Original String = HellO WOrlD
The Given String After Toggling Case = hELLo woRLd
Python Program to Toggle the String Cases using While Loop
This python toggle case program is the same as above. However, we just replaced the For Loop with Python while Loop.
# Python Program to Toggle Characters Case in a String
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))
elif(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 = ", string)
print("The Given String After Toggling Case = ", string1)
Python toggle string character cases output
Please Enter your Own String : TuTORiaL GAteWaY
Original String = TuTORiaL GAteWaY
The Given String After Toggling Case = tUtorIAl gaTEwAy
Python Program to Toggle case of Characters in a String Example 4
This python string conversion program is the same as the second example. However, we are using For Loop with Object.
Next, refer Python program to Convert to Uppercase, and Python program to Convert to Lowercase articles to understand the logic.
# Python Program to Toggle Characters Case in a String
string = input("Please Enter your Own String : ")
string1 = ''
for i in string:
if(i >= 'a' and i <= 'z'):
string1 = string1 + chr((ord(i) - 32))
elif(i >= 'A' and i <= 'Z'):
string1 = string1 + chr((ord(i) + 32))
else:
string1 = string1 + i
print("\nOriginal String = ", string)
print("The Given String After Toggling Case = ", string1)

Python Program to Toggle Case in a String using ASCII Values
In this program, we are comparing each character with ASCII values to find lowercase and uppercase characters in this string. If true, we are toggling their cases.
# Python Program to Toggle Characters Case in a String
string = input("Please Enter your Own String : ")
string1 = ''
for i in string:
if(ord(i) >= 65 and ord(i) <= 90):
string1 = string1 + chr((ord(i) + 32))
elif(ord(i) >= 97 and ord(i) <= 122):
string1 = string1 + chr((ord(i) - 32))
else:
string1 = string1 + i
print("\nOriginal String = ", string)
print("The Given String After Toggling Case = ", string1)
Python toggle string character cases output
Please Enter your Own String : ToGGLe StRINg CasEs
Original String = ToGGLe StRINg CasEs
The Given String After Toggling Case = tOgglE sTrinG cASeS
Also Read
- Python Program for ASCII Value of a Single Character
- Python program to print ASCII Value of Total Characters in a String