Reading Time: 1 minutes
Swap Cases in Python
Write a Python program to swap cases of an alphanumeric string.
Swap Cases
import string def swapCase(sourceString): newString = '' for character in sourceString: if character not in string.ascii_letters: newString += character elif character.isupper(): newString += character.lower() elif character.islower(): newString += character.upper() return newString swapCase('My name is Ethan, I am 23.') # 'mY NAME IS eTHAN, i AM 23.'