python - 我可以在打印语句中包含一个 for 循环吗?

标签 python python-3.x

我有一个事件的获奖者名单。我想在我的代码末尾打印它们,去掉括号和引号,我目前正在使用:

            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/

相关文章:

python - 功能修改原图

python - 在 GitLab 中导入 ZAP Docker 镜像

python - Pygame 碰撞检查错误

python - Jupyter 实验室错误地使用基本 python 可执行文件,jupyter 笔记本正确使用 venv 可执行文件?

python - 在 Tkinter 中运动期间避免事件抓取

python - Pandas Dataframe 到字典 groupby 索引

python - python 中 __truediv__ 的运算符重载

python - Struct.Error,必须是字节对象吗?

python-3.x - 如何使用仅支持 TLS 1.0 的主机处理 Python 请求

python - 在zc.buildout中,如何安装egg A,然后安装egg B,而egg B需要egg A才能正确安装?