Swap case of letters in string input parameter [closed]

核能气质少年 提交于 2019-12-03 15:11:27

EDIT - Way simpler than my original answer, and supports both ASCII and unicode. Thanks commenters.

a = 'aBcD'
a.swapcase()
>> AbCd

Original answer - disregard

a = 'aBcD'
''.join(map(str.swapcase, a))
>> AbCd

This will map the str.swapcase() function to each element of the string a, swapping the case of each character and returning an list of characters.

''.join() will join each character in the list into a new string.

You should look into string.maketrans to create a translation table that could be used with str.translate. Other constants which will probably be useful are string.ascii_lowercase and string.ascii_uppercase.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!