Python @ DjangoSpin

PyPro #69 Swap Cases

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 1 minutes

Swap Cases in Python

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.'

See also:

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon

Leave a Reply