This Python program reads a string and converts all lowercase letters to uppercase and all uppercase letters to lowercase.
The charChange() function iterates through each character in the string and builds a new string with the converted characters.
The final result is returned and displayed.
def charChange(str):
str1 = ""
for ch in str:
if ch.islower():
str1 += ch.upper()
elif ch.isupper():
str1 += ch.lower()
else:
str1 += ch
return str1
# Get input from user
input_text = input("Enter a string: ")
converted = charChange(input_text)
print("Converted string:", converted)
Enter a string: Hello World!
Converted string: hELLO wORLD!
- The charChange() function uses a for loop to examine each character
- If the character is lowercase, it’s converted to uppercase using upper()
- If the character is uppercase, it’s converted to lowercase using lower()
- Non-letter characters (spaces, punctuation) are added unchanged
- The final string is returned and printed