python - 如何在 Python 中打印带有字符串的列表?
问题描述
除了一些文本之外,我正在尝试打印我最喜欢的游戏列表。
代码:
favGames = ["The Division", "WoW", "Legend of Zelda", "Super Smash Bros."]
print("Here are some of my favorite games! " + favGames)
期望:我希望简单地得到一个打印行:
Here are some of my favorite games! The Division, WoW, Legend of Zelda, Super Smash Bros.
我收到以下错误消息:TypeError: can only concatenate list (not "str") to list
解决方案
尝试使用str.join
,并通过命令和空格加入它:
print("Here are some of my favorite games! " + ', '.join(favGames))
输出:
Here are some of my favorite games! The Division, WoW, Legend of Zelda, Super Smash Bros.
推荐阅读
- excel - 将范围内的多列堆叠为一列的 VBA 代码得到运行时错误 1004(粘贴和复制区域的大小和形状不同)
- javascript - 函数中的 JavaScript 图像预加载
- javascript - html 弹出窗口中的选择选项不起作用
- project - 如何使用 SuiteAnalytics Connect 访问 Netsuite 项目(recordtype=job)?
- powershell - 如何使用 Azure DevOps 在远程 Windows 机器上运行批处理文件,在目标机器上运行 Powershell?
- javascript - Ruby 和 JavaScript 中的符号是否具有相同的用途?
- graphql - Quarkus SmallRye Graphql-客户端变异查询
- heroku - 如何正确设置 DNS、Heroku 和 PointDNS
- php - 使用示例页面将自定义菜单项添加到 magento 2 管理侧边栏中的销售菜单选项卡
- azure-devops - 如何从嵌套对象中检索值?