簡體   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