Swap cases in a string

后端 未结 10 631
天涯浪人
天涯浪人 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:26

    def swap_case(s):
        l = list(s)
        for i in range(len(s)):
            if l[i].islower():
                l[i] = l[i].upper()
            else:
                l[i] = l[i].lower()
        k = "".join(map(str,l))
        return k
    

提交回复
热议问题