Swap cases in a string

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

    def swap_case(s):
        list_s= list(s)
        for index,char in enumerate(list_s):
            if char == char.lower():
                list_s[index] =char.upper()
            else:
                list_s[index]= char.lower()
                s = ''.join(list_s)
        return s
    
    string = 'HackerRank.com presents "Pythonist 2".'
    print(swap_case(string))
    

提交回复
热议问题