我有一个事件的获奖者名单。我想在我的代码末尾打印它们,去掉括号和引号,我目前正在使用:
for items in winners:
print(items)
我可以将其包含在打印语句中吗? 我要:
print("The winners of {} were: {} with a score of {}".format(sport, winners, max_result))
有没有一种方法可以将 for 循环集成到 print 语句中,或者是否有另一种方法可以消除引号和方括号以便我可以将其包含在语句中?
谢谢。
最佳答案
您不能包含 for 循环,但您可以将获胜者列表连接到一个字符串中。
winners = ['Foo', 'Bar', 'Baz']
print('the winners were {}.'.format(', '.join(winners)))
这将打印
the winners were Foo, Bar, Baz.
关于python - 我可以在打印语句中包含一个 for 循环吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39432063/