Return a string which is the concatenation of the strings in iterable.
A TypeError will be raised if there are any non-string values in
iterable, including bytes objects. The separator between elements is
the string providing this method.
def join(list_strings, delimiter):
str_result = ''
for e in list_strings[:-1]:
str_result += e + delimiter
if list_strings:
str_result += list_strings[-1]
return str_result
^{} 是来自
str
类的方法。你知道吗''.join(("Hello", "World"))
将返回'HelloWorld'
。你知道吗';'.join(("Hello", "World", "how", "are", "you"))
将返回'Hello;World;how;are;you'
。你知道吗如果需要在字符串列表(或任何iterable)中的每个元素之间添加分隔符,
join
非常有用。你知道吗看起来没什么,但如果不使用
join
,由于边缘效应,这种操作通常难以实现:对于字符串列表或元组:
对于任何iterable:
因为
join
适用于任何iterable,所以不需要从筛选结果创建列表。你知道吗同样有效
相关问题 更多 >
编程相关推荐