交换字符串中的大写和小写 [英] Swapping uppercase and lowercase in a string
本文介绍了交换字符串中的大写和小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将字符串的字符从小写改为大写.
我的代码在下面,我的代码得到的输出是a
;你能告诉我我错在哪里并解释原因吗?提前致谢
test = "AltERNating"def to_alternating_case(string):单词 = 列表(字符串)逐字逐句:如果 word.isupper() == True:返回 word.lower()别的:返回 word.upper()打印到_alternating_case(测试)
解决方案
如果你想反转那个字符串的大小写,试试这个:
<预><代码>>>>'交替'.swapcase()'交替'I would like to change the chars of a string from lowercase to uppercase.
My code is below, the output I get with my code is a
; could you please tell me where I am wrong and explain why?
Thanks in advance
test = "AltERNating"
def to_alternating_case(string):
words = list(string)
for word in words:
if word.isupper() == True:
return word.lower()
else:
return word.upper()
print to_alternating_case(test)
解决方案
If you want to invert the case of that string, try this:
>>> 'AltERNating'.swapcase()
'aLTernATING'
这篇关于交换字符串中的大写和小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文