我试图在以下列表中使用两个字符串,并希望使用一个字符串两次。我的意思是我不想在我的列表或元组中写两次同一个单词。
我的输出是 -
嗨,玛吉。 Maggie 我要去市场买铅笔和书。
嗨,露露。露露我要去市场买铅笔和书。
但我的错误是:元组索引超出范围 是否可以在我的元组中编写一次 maggie/lulu 并在两个地方使用它?
我的代码是:
first_tuple = ("magi","book","pencil")
second_tuple = ("lulu","copy","eraser")
together = (first_tuple, second_tuple)
New_List = list(together)
for i in range (len(New_List)):
print ("Hi, {}. {} I will go to market to buy {} and {} .".format(*New_List[i]))
最佳答案
使用
print ("Hi, {0}. {0} I will go to market to buy {1} and {2} .".format(*New_List[i]))
大括号中的数字是格式参数列表的索引。请参阅the docs了解详情。
关于python - 在Python列表中使用同一个字符串两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54283225/