The Wayback Machine - https://web.archive.org/web/20231110030216/https://www.geeksforgeeks.org/python-horizontal-concatenation-of-multiline-strings/
Open In App
Related Articles

Python – Horizontal Concatenation of Multiline Strings

Improve Article
Improve
Save Article
Save
Like Article
Like

Given pair of strings, which is multiline, perform concatenation horizontally.

Examples:

Input : 
test_str1 = ”’ 
geeks for 
geeks”’, 
test_str2 = ”’ 
is 
best”’ 
Output : 
geeks foris 
geeksbest 
Explanation : 1st line joined with 1st line of 2nd string, “geeks for” -> “is”.

Input : 
test_str1 = ”’ 
geeks for ”’ 
test_str2 = ”’ 
geeks ”’ 
Output : 
geeks for geeks 
Explanation : 1st line joined with 1st line of 2nd string, “geeks for ” -> “geeks”. 

Method #1 : Using zip() + split() + join() + list comprehension

In this, we perform the task of splitting by “\n” using split(), and they are paired together using zip(). The next step is joining both the zipped strings for horizontal orientation using “\n” and join().

Python3




# Python3 code to demonstrate working of
# Horizontal Concatenation of Multiline Strings
# Using zip() + split() + join() + list comprehension
 
# initializing strings
test_str1 = '''
geeks 4
geeks'''
 
test_str2 = '''
is
best'''
 
# printing original strings
print("The original string 1 is : " + str(test_str1))
print("The original string 2 is : " + str(test_str2))
 
# split lines
splt_lines = zip(test_str1.split('\n'), test_str2.split('\n'))
 
# horizontal join
res = '\n'.join([x + y for x, y in splt_lines])
 
# printing result
print("After String Horizontal Concatenation : " + str(res))


Output

The original string 1 is : 
geeks 4
geeks
The original string 2 is : 
is
best
After String Horizontal Concatenation : 
geeks 4is
geeksbest

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #2 : Using map() + operator.add + join()

In this, we use map() to with help of add() to perform concatenation, split() is used to perform initial split by “\n”.

Python3




# Python3 code to demonstrate working of
# Horizontal Concatenation of Multiline Strings
# Using map() + operator.add + join()
from operator import add
 
# initializing strings
test_str1 = '''
geeks 4
geeks'''
test_str2 = '''
is
best'''
 
# printing original strings
print("The original string 1 is : " + str(test_str1))
print("The original string 2 is : " + str(test_str2))
 
# using add to concat, map() to concat each lines zipped
res = '\n'.join(map(add, test_str1.split('\n'), test_str2.split('\n')))
 
# printing result
print("After String Horizontal Concatenation : " + str(res))


Output

The original string 1 is : 
geeks 4
geeks
The original string 2 is : 
is
best
After String Horizontal Concatenation : 
geeks 4is
geeksbest

Time Complexity: O(n) -> ( join function)
Auxiliary Space: O(n)

Method 3: using the itertools.zip_longest() function, split() function, and a for loop.

Step-by-step approach:

  1. Import the itertools module.
  2. Initialize the strings as multiline strings.
  3. Split the strings using the split() function and store them in separate variables.
  4. Create a list comprehension to join the corresponding lines from both strings together horizontally.
  5. Join the elements in the list comprehension using the join() function and store them in a new variable.
  6. Print the resulting string.

Below is the implementation of the above approach:

Python3




# Python3 code to demonstrate working of
# Horizontal Concatenation of Multiline Strings
# Using itertools.zip_longest() and for loop
 
# import itertools module
import itertools
 
# initializing strings
test_str1 = '''
geeks 4
geeks'''
 
test_str2 = '''
is
best'''
 
# printing original strings
print("The original string 1 is : " + str(test_str1))
print("The original string 2 is : " + str(test_str2))
 
# split the strings
test_list1 = test_str1.split('\n')
test_list2 = test_str2.split('\n')
 
# join corresponding lines horizontally
result = ""
for line1, line2 in itertools.zip_longest(test_list1, test_list2, fillvalue=""):
    result += line1 + line2 + "\n"
 
# printing result
print("After String Horizontal Concatenation : " + str(result))


Output

The original string 1 is : 
geeks 4
geeks
The original string 2 is : 
is
best
After String Horizontal Concatenation : 
geeks 4is
geeksbest

Time complexity: O(n), where n is the total number of characters in both strings.
Auxiliary space: O(n), where n is the total number of characters in both strings.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 17 Apr, 2023
Like Article
Save Article
Similar Reads
Related Tutorials