Python concatenate string & list

前端 未结 5 1914
清酒与你
清酒与你 2020-12-14 08:08

I have a list and string:

fruits = [\'banana\', \'apple\', \'plum\']
mystr = \'i like the following fruits: \'

How can I concatenate them s

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-14 08:31

    You can use str.join.

    result = "i like the following fruits: "+', '.join(fruits)
    

    (assuming fruits only contains strings). If fruits contains a non-string, you can convert it easily by creating a generator expression on the fly:

    ', '.join(str(f) for f in fruits)
    

提交回复
热议问题