Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swapping uppercase and lowercase in a string

I would like to change the chars of a string from lowercase to uppercase.

My code is below, the output I get with my code is a; could you please tell me where I am wrong and explain why? Thanks in advance

test = "AltERNating"

def to_alternating_case(string):
    words = list(string)
    for word in words:
        if word.isupper() == True:
            return word.lower()
        else:
            return word.upper()  

print to_alternating_case(test)
like image 758
Marco Giuseppe de Pinto Avatar asked Mar 27 '16 12:03

Marco Giuseppe de Pinto


1 Answers

If you want to invert the case of that string, try this:

>>> 'AltERNating'.swapcase()
'aLTernATING'
like image 68
folkol Avatar answered Oct 22 '22 00:10

folkol