简体   繁体   中英

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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