How to concatenate items in a list to a single string?

后端 未结 11 2010
北荒
北荒 2020-11-21 05:59

Is there a simpler way to concatenate string items in a list into a single string? Can I use the str.join() function?

E.g. this is the input [\'t

11条回答
  •  南笙
    南笙 (楼主)
    2020-11-21 06:09

    list = ['aaa', 'bbb', 'ccc']
    
    string = ''.join(list)
    print(string)
    >>> aaabbbccc
    
    string = ','.join(list)
    print(string)
    >>> aaa,bbb,ccc
    
    string = '-'.join(list)
    print(string)
    >>> aaa-bbb-ccc
    
    string = '\n'.join(list)
    print(string)
    >>> aaa
    >>> bbb
    >>> ccc
    

提交回复
热议问题