我试图将列表中的项连接到字符串上.
list = ['a', 'b', 'c', 'd']
string = ''
for i in list:
string.join(str(i))
Run Code Online (Sandbox Code Playgroud)
你不需要循环:
items = ['a', 'b', 'c', 'd']
result = "".join(items)
Run Code Online (Sandbox Code Playgroud)
请注意,使用list变量名称是个坏主意,因为这会阻止您使用list内置类型.
这是你想要的?
>>> my_list = ['a', 'b', 'c', 'd']
>>> "".join(my_list)
'abcd'
Run Code Online (Sandbox Code Playgroud)
您不应该将其list用作变量名,因为这会影响内置类list.