Friday, December 27, 2024
Google search engine
HomeLanguagesPython – Horizontal Concatenation of Multiline Strings

Python – Horizontal Concatenation of Multiline Strings

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

Examples:

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

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

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 = '''
Lazyroar 4
Lazyroar'''
 
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 : 
Lazyroar 4
Lazyroar
The original string 2 is : 
is
best
After String Horizontal Concatenation : 
Lazyroar 4is
Lazyroarbest

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 = '''
Lazyroar 4
Lazyroar'''
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 : 
Lazyroar 4
Lazyroar
The original string 2 is : 
is
best
After String Horizontal Concatenation : 
Lazyroar 4is
Lazyroarbest

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 = '''
Lazyroar 4
Lazyroar'''
 
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 : 
Lazyroar 4
Lazyroar
The original string 2 is : 
is
best
After String Horizontal Concatenation : 
Lazyroar 4is
Lazyroarbest

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.

RELATED ARTICLES

Most Popular

Recent Comments