Thursday, March 24, 2022

Case swapping Python Codewars

Task:

Given a string, swap the case for each of the letters.

e.g. CodEwArs --> cODeWaRS

Examples
""           ->   ""
"CodeWars"   ->   "cODEwARS"
"abc"        ->   "ABC"
"ABC"        ->   "abc"
"123235"     ->   "123235"

Solution:


def swap(string_):
    new = ''
    for i in string_:
        if i.islower():
            i = i.upper()
        else:
            i = i.lower()
        new += ''.join(i)
            
    return new

print(swap('HelloWorld'))
Training on Case swapping | Codewars


Related Posts


No comments :

Post a Comment