简体   繁体   English

切换大小写 python

[英]Toggle uppercase and lowercase python

how do I make the program ask for a name and then alternate the result in uppercase and lowercase?如何让程序询问名称,然后将结果交替显示为大写和小写?

Example:例子:

How is your name?你的名字怎么样? george乔治

Hello GeOrGe你好乔治

Thanks谢谢

print("How is your name?");
name = input();
list(name)
letters = list(name)
final_word = ""
for x in range(len(name)):
    if x % 2 == 0:
        final_word += letters[x].upper()
    else:
        final_word += letters[x].lower()
else:
    print("Hello" + final_word)

I think this code should work我认为这段代码应该有效

you can use list comprehension -你可以使用list comprehension -

s =  input()
s  = ''.join(item.upper() if index%2 ==0  else item.lower() for index,item in enumerate(s))

how do I make the program ask for a name and then alternate the result in uppercase and lowercase?如何让程序询问名称,然后以大写和小写形式交替显示结果?

Example:例子:

How is your name?你的名字怎么样? george乔治

Hello GeOrGe你好乔治

Thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM