Swap cases in a string

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

    def swap_case(s):
        return s.swapcase()
    
    #or you can use list comprehension 
    
    def swap_case(s):
        new=[ch.lower() if ch.isupper() else ch.upper() for ch in s]
        new=''.join(new)
        return new
    
    if __name__ == '__main__':
        s = input()
        result = swap_case(s)
        print(result)
    

提交回复
热议问题