从列表条目中连接python字符串

ved*_*905 12 python string python-2.7

假设我有一个字符串列表,我想将它们组合成一个由下划线分隔的单个字符串.我知道我可以使用循环来做到这一点,但python做了很多没有循环的事情.python中有什么东西已经有这个功能吗?例如,我有:

string_list = ['Hello', 'there', 'how', 'are', 'you?']
Run Code Online (Sandbox Code Playgroud)

我想制作一个单独的字符串:

'Hello_there_how_are_you?'
Run Code Online (Sandbox Code Playgroud)

我尝试过的:

mystr = ''    
mystr.join(string_list+'_')
Run Code Online (Sandbox Code Playgroud)

但这给出了一个"TypeError:只能连接列表(不是"str")列表".我知道这样简单,但不是很明显.

Mar*_*ers 24

您使用加入字符加入列表:

string_list = ['Hello', 'there', 'how', 'are', 'you?']
'_'.join(string_list)
Run Code Online (Sandbox Code Playgroud)

演示:

>>> string_list = ['Hello', 'there', 'how', 'are', 'you?']
>>> '_'.join(string_list)
'Hello_there_how_are_you?'
Run Code Online (Sandbox Code Playgroud)