Swap cases in a string

后端 未结 10 613
天涯浪人
天涯浪人 2021-01-21 15:20

I\'m trying to solve this challenge in hackerrank, which asks to convert all lowercase letters to uppercase letters and vice versa.

I attempt it with the following code:

10条回答
  •  有刺的猬
    2021-01-21 15:38

    Try this

    def swap_case(s):
        l =[]
        str1 = ''
        for i in s:
            if i.isupper():
    
                l.append(i.lower())
            elif i.islower():
                l.append(i.upper())
            else:
                l.append(i)
        return (str1.join(l))
    if __name__ == '__main__':
        s = input()
        result = swap_case(s)
        print(result)
    

提交回复
热议问题