【问题标题】:I can't figure out where is my mistake, can anyone help me with it? (Python) [duplicate]我不知道我的错误在哪里,有人可以帮我解决吗? (Python)[重复]
【发布时间】:2020-11-20 22:18:02
【问题描述】:
# 1. Create a list of all of the names
name = ["kaki", "helen", "makir", "pallu"]

print("My cousins are" + name+ "but helen is different than them.")

错误:

 ----- print("My cousins are" + name+ "but helen is different than them.")
TypeError: can only concatenate str (not "list") to str. -----

【问题讨论】:

  • 你不能把列表类型放到你的字符串中
  • 您的错误与错误消息中报告的完全一样,您的代码正在将 (+) list [...] 添加到 str "My cousins are",这是不可能的因为它们是不同的类型。
  • print("My cousins are " + ','.join(name)+ " but helen is different than them.") print("My cousins are " + str(name)[1:-1]+ " but helen is different than them.") 您正在将列表连接到字符串。

标签: python xcode list variables rename


【解决方案1】:

试试:

print("My cousins are " + str(name) + " but helen is different than them.")

【讨论】:

    【解决方案2】:

    不要与+连接,只需改为逗号即可:

    name = ["kaki", "helen", "makir", "pallu"]
    
    print("My cousins are", name, "but helen is different than them.")
    

    这是输出:

    My cousins are ['kaki', 'helen', 'makir', 'pallu'] but helen is different than them.
    

    【讨论】:

      猜你喜欢
      • 2021-07-10
      • 2016-07-09
      • 1970-01-01
      • 1970-01-01
      • 2021-09-22
      • 2019-09-23
      • 2015-02-25
      • 1970-01-01
      相关资源
      最近更新 更多