【问题标题】:How can I print a list with a string in Python? [duplicate]如何在 Python 中打印带有字符串的列表? [复制]
【发布时间】:2019-07-25 02:42:46
【问题描述】:

除了一些文字之外,我正在尝试打印我最喜欢的游戏列表。

代码:

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:只能将列表(不是“str”)连接到列表

【问题讨论】:

    标签: python string list printing


    【解决方案1】:

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

    【讨论】:

    • 谢谢!!这绝对对我有用;我将研究加入声明。刚开始诚实编码
    • Ebeeze 如果您觉得@U10-Forward 回答了您的问题,那么您应该接受他的回答。
    • @KingMak - 不确定你指的是什么;我在此页面上没有看到这样的选项。
    • @Ebeeze 看看here
    猜你喜欢
    • 2022-11-04
    • 2019-06-29
    • 2016-07-17
    • 1970-01-01
    • 2018-09-02
    • 1970-01-01
    • 2020-03-01
    • 2017-09-16
    • 2021-04-26
    相关资源
    最近更新 更多