Write a Python program to swap cases of a given string


The program takes a string as input, and then performs a character-wise transformation on the input string, where each uppercase character is converted to lowercase, and each lowercase character is converted to uppercase. The transformed string is then printed to the console. The input string is stored in the variable str. The program initializes an empty string res, which will store the transformed string.

The program then enters a loop that iterates over each character in the input string. For each character, the program checks if it is uppercase by calling the isupper() method on the character. If the character is uppercase, the program converts it to lowercase by calling the lower() method on the character, and then appends the transformed character to the res string using the += operator. If the character is lowercase, the program converts it to uppercase by calling the upper() method on the character, and then appends the transformed character to the res string.

After all characters in the input string have been transformed and appended to the res string, the transformed string is printed to the console using the print() function.

Source Code

str="Tutor Joes Computer Education"
res = ""   
for ch in str:
   if ch.isupper():
	   res += ch.lower()
   else:
	   res += ch.upper()           
print(res)

Output

tUTOR jOES cOMPUTER eDUCATION


Example Programs