连接两个字符串元素明智
我遇到了一个问题,我不得不加入两个字符串元素明智 对于ex:
str1 = "ABCD"
str2 = "EFGH"
output = "AEBFCGDH"
我写了此代码
op = ''
op = op.join([i + j for i, j in zip(str1, str2)])
print(op)
,它奏效了,但我想知道两个字符串的长度是否不同 对于Ex:
str1 = "ABC"
str2 = "DEFGH"
output = "ADBECFGH"
or
str1 = "ABCDG"
str2 = "DEF"
output = "ADBECFDG"
如何为此编码?
I came across a problem in which i had to concatenate two string element wise
for ex :
str1 = "ABCD"
str2 = "EFGH"
output = "AEBFCGDH"
I wrote this code
op = ''
op = op.join([i + j for i, j in zip(str1, str2)])
print(op)
And it worked but I was wondering if the length of two strings is different
for ex:
str1 = "ABC"
str2 = "DEFGH"
output = "ADBECFGH"
or
str1 = "ABCDG"
str2 = "DEF"
output = "ADBECFDG"
How do I code for this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需切换
zip()
带有zip_longest()
:Just switch
zip()
withzip_longest()
:您可以取较短的字符串,拉链直至其长度,然后将较长字符串的其余部分串联。
例如:
Zip()仅拉链,直到两个迭代的较短。
请参阅: https://docs.pypython.org/3.3/library/functions。 html#zip
You could take the shorter string, zip until its length and then concatenate the remaining part of the longer string.
eg:
zip() only zips until the shorter of the two iterables.
See: https://docs.python.org/3.3/library/functions.html#zip
简单解决方案:
Simple solution: