2024-04-19 20:19:11 发布
网友
如何在python中用一个字符连接两个或多个字符串?你知道吗
例如
a = 'hello' b = 'world' output = 'hweolellod'
三个或更多的字符串也是如此。使用+是没有帮助的。你知道吗
+
一种方法是将str.join与itertools一起使用:
str.join
itertools
from itertools import chain, zip_longest a = 'hello' b = 'world' zipper = zip_longest(a, b, fillvalue='') res = ''.join(list(chain.from_iterable(zipper))) print(res) hweolrllod
解释
zip_longest
zipper
a
b
你可以试试这个:
''.join([x + y for x, y in zip(a, b)])
它给出:
'hweolrllod'
一种方法是将
str.join
与itertools
一起使用:解释
zip_longest
用于解释长度不一致的字符串。你知道吗zipper
这里是一个惰性迭代器,它通过索引同时迭代a
和b
的每个字符。你知道吗str.join
更有效。你知道吗你可以试试这个:
它给出:
相关问题 更多 >
编程相关推荐