【发布时间】:2022-01-11 04:33:03
【问题描述】:
我是 Python 编程新手。我正在尝试了解列表。
我想创建一个新程序,可以将句子中的每个单词交替转换为大写和小写字母。
我被困在这里:
input_text = "hello my name is rahul and i'm from india"
result = ""
myArr = input_text.split(' ')
for idx in range(len(myArr)):
if idx % 2 == 0 :
result = result + myArr[idx].upper()
else:
result = result + myArr[idx].lower()
print(str(result))
通过这段代码我可以得到,例如:
input : "hello my name is rahul and i'm from india"
output : "HELLOmyNAMEisRAHULandI'MfromINDIA"
但我想要得到的是:
input : "hello my name is rahul and i'm from india"
output : "HELLO my NAME is RAHUL and I'M from INDIA"
我想为句子中的每个单词添加一个空格。
【问题讨论】:
标签: python arrays list uppercase lowercase