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
test_str1 =
test_str2 =
print("The original string 1 is : " + str(test_str1))
print("The original string 2 is : " + str(test_str2))
splt_lines = zip(test_str1.split('\n'), test_str2.split('\n'))
res = '\n'.join([x + y for x, y in splt_lines])
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
from operator import add
test_str1 =
test_str2 =
print("The original string 1 is : " + str(test_str1))
print("The original string 2 is : " + str(test_str2))
res = '\n'.join(map(add, test_str1.split('\n'), test_str2.split('\n')))
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:
- Import the itertools module.
- Initialize the strings as multiline strings.
- Split the strings using the split() function and store them in separate variables.
- Create a list comprehension to join the corresponding lines from both strings together horizontally.
- Join the elements in the list comprehension using the join() function and store them in a new variable.
- Print the resulting string.
Below is the implementation of the above approach:
Python3
import itertools
test_str1 =
test_str2 =
print("The original string 1 is : " + str(test_str1))
print("The original string 2 is : " + str(test_str2))
test_list1 = test_str1.split('\n')
test_list2 = test_str2.split('\n')
result = ""
for line1, line2 in itertools.zip_longest(test_list1, test_list2, fillvalue=""):
result += line1 + line2 + "\n"
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!