【发布时间】: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