Swap cases in a string

后端 未结 10 623
天涯浪人
天涯浪人 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):
        new = ""
        for i in range(len(s)):
            if str.isupper(s[i]):
                new = new + str.lower(s[i])
    
            elif str.islower(s[i]):
                new = new + str.upper(s[i])
    
            else:
                new = new + str(s[i])
            
        return (new)
    
    if __name__ == '__main__':
        s = input()
        result = swap_case(s)
        print(result)
    

提交回复
热议问题