Swap cases in a string

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

    As @Julien stated in comment upper and lower methods return a copy, and do not change the object itself. See this docs.

    EDIT @aryamccarthy reminded me of already existing feature for this kind of task in python: swapcase() method. See more here. Note this also returns a copy of the string.

提交回复
热议问题