Open In App

Python – Horizontal Concatenation of Multiline Strings

Last Updated : 16 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Horizontal concatenation of multiline strings involves merging corresponding lines from multiple strings side by side using methods like splitlines() and zip(). Tools like itertools.zip_longest() help handle unequal lengths by filling missing values, and list comprehensions format the result.

Using zip() and List Comprehension

zip() transposes the rows and columns of the 2D list m. List comprehension flattens the transposed rows into a single list of strings which are then joined with spaces.

Python
s1 = '''Hello 
How are you?'''

s2 = '''Good 
I'm fine.'''

s3 = '''Thank you 
See you later.'''

# Split strings into lines
a = s1.splitlines()
b = s2.splitlines()
c = s3.splitlines()

# Concatenate lines horizontally
result = [f"{line1} {line2} {line3}" for line1, line2, line3 in zip(a, b, c)]

# Print result
for line in result:
    print(line)

Output
Hello  Good  Thank you 
How are you? I'm fine. See you later.

Explanation:

  • Splitlines() method splits each string into a list of lines, and zip() is used to pair corresponding lines from s1, s2, and s3 together.
  • List comprehension combines each set of lines into a single string horizontally concatenating them.

Using zip() with map()

Using zip() with map(), we pair the corresponding lines from a, b, and c and concatenate them horizontally map() function applies a lambda function that formats each triplet of lines with spaces between them.

Python
s1 = '''Hello 
How are you?'''

s2 = '''Good 
I'm fine.'''

s3 = '''Thank you 
See you later.'''

# Split strings into lines and concatenate horizontally
a = s1.splitlines()
b = s2.splitlines()
c = s3.splitlines()

result = map(lambda x: f"{x[0]} {x[1]} {x[2]}", zip(a, b, c))

# Print result
for line in result:
    print(line)

Output
Hello  Good  Thank you 
How are you? I'm fine. See you later.

Explanation:

  • splitlines() method splits each string into a list of lines and zip() pairs the corresponding lines from a, b, and c.
  • map() function uses a lambda to format each triplet of lines by concatenating them with spaces, and the result is printed line by line.

Using itertools.zip_longest()

itertools.zip_longest() pairs elements from the input lists filling missing values with a specified fill value and can be used to concatenate lines horizontally even if the lists have unequal lengths.

Python
import itertools

s1 = '''Hello 
How are you?'''

s2 = '''Good 
I'm fine.'''

s3 = '''Thank you 
See you later.'''

# Split strings into lines
a = s1.splitlines()
b = s2.splitlines()
c = s3.splitlines()

# Use zip_longest to handle unequal lengths by filling with empty strings
result = [
    f"{line1} {line2} {line3}" 
    for line1, line2, line3 in itertools.zip_longest(a, b, c, fillvalue='')
]

# Print result
for line in result:
    print(line)

Output
Hello  Good  Thank you 
How are you? I'm fine. See you later.

Explanation:

  • itertools.zip_longest() pairs the lines from a, b, and c, filling any shorter lists with empty strings, ensuring all lines are processed even if the lists have unequal lengths.
  • List comprehension formats each triplet of lines by concatenating them with spaces.


Next Article
Practice Tags :

Similar Reads