有没有办法打印出句子中嵌入的列表的值而不单独调用它们?例如,而不是这样做:
test = ["baseball","brother","sister"]
print ("Bob went out to play {} with his {} and {}.".format(test[0], test[1], test[2])).
有没有办法将其缩短为:
print ("Bob went out to play {} with his {} and {}.".format(test[0:2]))
答案 0 :(得分:1)
正如Paul Panzer在问题评论中指出的那样,你可以在前面使用*
,但是你需要*test[0:3]
而不是*test[0:2]
:
test = ["baseball","brother","sister"]
print ("Bob went out to play {} with his {} and {}.".format(*test[0:3]))
这会产生:
Bob went out to play baseball with his brother and sister.