首页 > 解决方案 > 如何在 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

标签: pythonstringlistprinting

解决方案


尝试使用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.

推荐阅读