连接列表条目中的 python 字符串[重复]

2023-12-25

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

string_list = ['Hello', 'there', 'how', 'are', 'you?']

我想制作一个字符串,例如:

'Hello_there_how_are_you?'

我尝试过的:

mystr = ''    
mystr.join(string_list+'_')

但这给出了“类型错误:只能将列表(而不是“str”)连接到列表”。我知道它是这样简单的事情,但它并不是立即显而易见的。


您使用加入角色加入列表:

string_list = ['Hello', 'there', 'how', 'are', 'you?']
'_'.join(string_list)

Demo:

>>> string_list = ['Hello', 'there', 'how', 'are', 'you?']
>>> '_'.join(string_list)
'Hello_there_how_are_you?'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

连接列表条目中的 python 字符串[重复] 的相关文章

随机推荐