简体   繁体   English

列表中的python项目已添加到字符串

[英]python items in list added to string

I am trying to concatenate items in a list onto a string. 我正在尝试将列表中的项目连接到字符串上。

list = ['a', 'b', 'c', 'd']
string = ''
for i in list:
    string.join(str(i))

You don't need a loop: 您不需要循环:

items = ['a', 'b', 'c', 'd']
result = "".join(items)

Note that it's a bad idea to use list as the name of a variable, because that prevents you from using list to mean the built-in type. 请注意,将list用作变量的名称是一个坏主意,因为这会阻止您使用list来表示内置类型。

Is this what you are looking for? 这是你想要的?

>>> my_list = ['a', 'b', 'c', 'd']
>>> "".join(my_list)
'abcd'

You shouldn't use list as a variable name, since this will shadow the built-in class list . 您不应该将list用作变量名,因为这将使内置类list蒙上阴影。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM