Python无法将'list'对象转换为str错误

Exc*_*era 10 python string list typeerror python-3.x

我正在使用最新的Python 3

letters = ['a', 'b', 'c', 'd', 'e']
letters[:3]
print((letters)[:3])
letters[3:]
print((letters)[3:])
print("Here is the whole thing :" + letters)
Run Code Online (Sandbox Code Playgroud)

错误:

Traceback (most recent call last):
  File "C:/Users/Computer/Desktop/Testing.py", line 6, in <module>
    print("Here is the whole thing :" + letters)
TypeError: Can't convert 'list' object to str implicitly
Run Code Online (Sandbox Code Playgroud)

修复时,请解释它是如何工作的:)我不想复制固定的行

mu *_*u 無 16

就目前而言,您正在尝试将字符串与最终print语句中的列表连接起来,这将抛出TypeError.

而是将您的上一个print语句更改为以下之一:

print("Here is the whole thing :" + ' '.join(letters)) #create a string from elements
print("Here is the whole thing :" + str(letters)) #cast list to string
Run Code Online (Sandbox Code Playgroud)