Lazloo Xp
Lazloo Xp

Reputation: 988

Horizontal concatenation of two multiline strings

I currently try to concatenate two multiline string horizontally. For example, having two strings str_a and str_b

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

With the print return

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

and

A
B

The print return of the resulting string after the horizontal concatenation should look like

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

Upvotes: 2

Views: 740

Answers (1)

lmiguelvargasf
lmiguelvargasf

Reputation: 69735

Use this:

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 

Upvotes: 4

Related Questions