Python – Triple quote String concatenation
Last Updated :
18 May, 2023
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
test_str1 =
test_str2 =
print ( "The original string 1 is : " + test_str1)
print ( "The original string 2 is : " + test_str2)
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)
print ( "String after concatenation : " + str (res))
|
Output :
The original string 1 is : gfg
is
The original string 2 is : best
for geeks
String after concatenation : gfg best
is for geeks
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 =
str2 =
print (concatenate_strings(str1, str2))
|
Output
gfg is best for geeks
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 =
str2 =
print (concatenate_strings(str1, str2))
|
Output
gfg is best for geeks
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
test_str1 =
test_str2 =
print ( "The original string 1 is : " + test_str1)
print ( "The original string 2 is : " + test_str2)
res = '\n' .join([ " " + i.strip() + " " + j.strip()
for i, j in zip (test_str1.splitlines(), test_str2.splitlines())])
print ( "String after concatenation : " + str (res))
|
Output
The original string 1 is : gfg
is
The original string 2 is : best
for geeks
String after concatenation : gfg best
is for geeks
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().
Similar Reads
Python - Concatenation of two String Tuples
Sometimes, while working with records, we can have a problem in which we may need to perform String concatenation of tuples. This problem can occur in day-day programming. Letâs discuss certain ways in which this task can be performed. Method #1 : Using zip() + generator expression The combination o
3 min read
Convert tuple to string in Python
The goal is to convert the elements of a tuple into a single string, with each element joined by a specific separator, such as a space or no separator at all. For example, in the tuple ('Learn', 'Python', 'Programming'), we aim to convert it into the string "Learn Python Programming". Let's explore
3 min read
Python - Substring concatenation by Separator
Sometimes, while working with Python Lists, we can have problem in which we need to perform the concatenation of strings in a list till the separator. This can have application in domains in which we need chunked data. Lets discuss certain ways in which this task can be performed. Method #1 : Using
5 min read
Printing String with double quotes - Python
Printing a string with double quotes means displaying a string enclosed in double quotes (") as part of the output. This can be helpful when we want to make it clear that the text itself is a string or when quotes are essential to the context. Using Escape Characters (\")Escape the double quotes ins
3 min read
Convert String to Tuple - Python
When we want to break down a string into its individual characters and store each character as an element in a tuple, we can use the tuple() function directly on the string. Strings in Python are iterable, which means that when we pass a string to the tuple() function, it iterates over each characte
2 min read
Python - Remove Punctuation from String
In this article, we will explore various methods to Remove Punctuations from a string. Using str.translate() with str.maketrans()str.translate() method combined with is str.maketrans() one of the fastest ways to remove punctuation from a string because it works directly with string translation table
2 min read
Convert JSON to string - Python
Data is transmitted across platforms using API calls. Data is mostly retrieved in JSON format. We can convert the obtained JSON data into String data for the ease of storing and working with it. Python provides built-in support for working with JSON through the json module. We can convert JSON data
2 min read
Python - Convert Dictionary to Concatenated String
In Python, sometimes we need to convert a dictionary into a concatenated string. The keys and values of the dictionary are combined in a specific format to produce the desired string output. For example, given the dictionary {'a': 1, 'b': 2, 'c': 3}, we may want the string "a:1, b:2, c:3". Let's dis
3 min read
Python - Concatenate Tuple to Dictionary Key
Given Tuples, convert them to the dictionary with key being concatenated string. Input : test_list = [(("gfg", "is", "best"), 10), (("gfg", "for", "cs"), 15)] Output : {'gfg is best': 10, 'gfg for cs': 15} Explanation : Tuple strings concatenated as strings. Input : test_list = [(("gfg", "is", "best
6 min read
Python | How to Concatenate tuples to nested tuples
Sometimes, while working with tuples, we can have a problem in which we need to convert individual records into a nested collection yet remaining as separate element. Usual addition of tuples, generally adds the contents and hence flattens the resultant container, this is usually undesired. Let's di
6 min read