繁体   English   中英

两个多行字符串的水平连接

[英]Horizontal concatenation of two multiline strings

我目前尝试水平连接两个多行字符串。 例如,有两个字符串 str_a 和 str_b

str_a = """This is row \nAnd this is row\nThis is the final row"""
str_b = """A\nB"""

随着打印返回

This is row 
And this is row 
This is the final row 

A
B

水平连接后结果字符串的打印返回应如下所示

This is row A
And this is row B
This is the final row

用这个:

import itertools

for a, b in itertools.zip_longest(str_a.split('\n'), str_b.split('\n')):
    print(a, b if b else '')

Output:

This is row  A
And this is row B
This is the final row 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM