Sometimes, while working with Python Strings, we can have a problem in which we need to perform concatenation of Strings which are constructed by Triple quotes. This happens in cases we have multiline strings. This can have applications in many domains. Let us discuss certain ways in which this task can be performed.
Input : test_str1 = """mango is""" test_str2 = """good for health """ Output : mango good is for health
Input : test_str1 = """Gold is""" test_str2 = """important for economy """ Output : Gold important is for economy
Method : Using splitlines() + strip() + join()
The combination of the above functions can be used to perform this task. In this, we perform the ask of line splitting using splitlines(). The task of concatenation is done using strip() and join().
Python3
# Python3 code to demonstrate working of # Triple quote String concatenation # Using splitlines() + join() + strip() # initializing strings test_str1 = """gfg is""" test_str2 = """best for Lazyroar """ # printing original strings print ( "The original string 1 is : " + test_str1) print ( "The original string 2 is : " + test_str2) # Triple quote String concatenation # Using splitlines() + join() + strip() test_str1 = test_str1.splitlines() test_str2 = test_str2.splitlines() res = [] for i, j in zip (test_str1, test_str2): res.append( " " + i.strip() + " " + j.strip()) res = '\n' .join(res) # printing result print ( "String after concatenation : " + str (res)) |
The original string 1 is : gfg is The original string 2 is : best for Lazyroar String after concatenation : gfg best is for Lazyroar
Time Complexity: O(n)
Auxiliary Space: O(n)
Approach#2: Using formatting
We can use string formatting to concatenate the two triple-quoted strings.
Algorithm
1. This code defines a function named concatenate_strings that takes two string arguments, str1 and str2.
2. The function creates a new string variable named concatenated_string, which concatenates the stripped versions of str1 and str2 with a space in between using an f-string.
3. The strip() method removes any leading and trailing whitespaces from the strings.
4. Finally, the function returns the concatenated string.
Python3
def concatenate_strings(str1, str2): concatenated_string = f "{str1.strip()} {str2.strip()}" return concatenated_string str1 = """gfg is""" str2 = """best for Lazyroar""" print (concatenate_strings(str1, str2)) |
gfg is best for Lazyroar
Time complexity: O(n), where n is the length of the two input strings.
Auxiliary Space: O(n), since we are creating new strings to concatenate the input strings.
Method 3: Using the “+” operator
Define a function named “concatenate_strings” that takes in two parameters, str1 and str2.
Inside the function, concatenate the two strings using the “+” operator and store the result in a new variable named “concatenated_string”.
Strip the whitespace from both ends of the concatenated string using the “strip()” method.
Return the stripped concatenated string from the function.
Python3
def concatenate_strings(str1, str2): concatenated_string = str1 + " " + str2 concatenated_string = concatenated_string.strip() return concatenated_string str1 = """gfg is""" str2 = """best for Lazyroar""" print (concatenate_strings(str1, str2)) |
gfg is best for Lazyroar
Time complexity: O(1)
Auxiliary space: O(n), where n is the length of the concatenated string.
Method 4: Using list comprehension and join()
Python3
# Python3 code to demonstrate working of # Triple quote String concatenation # Using list comprehension and join() # Initializing strings test_str1 = """gfg is""" test_str2 = """best for Lazyroar """ # Printing original strings print ( "The original string 1 is : " + test_str1) print ( "The original string 2 is : " + test_str2) # Triple quote String concatenation # using list comprehension and join() res = '\n' .join([ " " + i.strip() + " " + j.strip() for i, j in zip (test_str1.splitlines(), test_str2.splitlines())]) # Printing result print ( "String after concatenation : " + str (res)) |
The original string 1 is : gfg is The original string 2 is : best for Lazyroar String after concatenation : gfg best is for Lazyroar
Time complexity: O(n), where n is the number of lines in the input strings test_str1 and test_str2. The zip() function and list comprehension iterate over the lines, and the join() operation joins the resulting lines.
Auxiliary space: O(n), where n is the number of lines. The res list stores the concatenated lines before joining them with join().