How would you make a comma-separated string from a list of strings?

后端 未结 14 1181
北海茫月
北海茫月 2020-11-22 16:07

What would be your preferred way to concatenate strings from a sequence such that between every two consecutive pairs a comma is added. That is, how do you map, for instance

14条回答
  •  遥遥无期
    2020-11-22 16:36

    my_list = ['a', 'b', 'c', 'd']
    my_string = ','.join(my_list)
    
    'a,b,c,d'
    

    This won't work if the list contains integers


    And if the list contains non-string types (such as integers, floats, bools, None) then do:

    my_string = ','.join(map(str, my_list)) 
    

提交回复
热议问题