Python – Join strings by multiple delimiters
Last Updated :
22 Apr, 2023
Given two strings, the task is to write a python program to join them by every delimiter from delimiter list.
Input : test_str1 = ‘Geeksforgeeks’, test_str2 = “Best”, join_list = [“+”, “*”, “-“, “$”, “,”, “@”]
Output : [‘Geeksforgeeks+Best’, ‘Geeksforgeeks*Best’, ‘Geeksforgeeks-Best’, ‘Geeksforgeeks$Best’, ‘Geeksforgeeks,Best’, ‘Geeksforgeeks@Best’]
Explanation : Elements are concatenated with all desired delimiters.
Input : test_str1 = ‘Geeksforgeeks’, test_str2 = “Best”, join_list = [“+”, “*”, “-“, “$”]
Output : [‘Geeksforgeeks+Best’, ‘Geeksforgeeks*Best’, ‘Geeksforgeeks-Best’, ‘Geeksforgeeks$Best’]
Explanation : Elements are concatenated with all desired delimiters.
Method 1 : Using list comprehension
In this, we iterate through all the delimiters from list using loop inside list comprehension and + operator performs task of concatenation.
Example:
Python3
test_str1 = 'Geeksforgeeks'
test_str2 = "Best"
print ( "The original string 1 is : " + str (test_str1))
print ( "The original string 2 is : " + str (test_str2))
join_list = [ "+" , "*" , "-" , "$" , "," , "@" ]
res = [test_str1 + delim + test_str2 for delim in join_list]
print ( "All delimiters concatenations : " + str (res))
|
Output:
The original string 1 is : Geeksforgeeks
The original string 2 is : Best
All delimiters concatenations : [‘Geeksforgeeks+Best’, ‘Geeksforgeeks*Best’, ‘Geeksforgeeks-Best’, ‘Geeksforgeeks$Best’, ‘Geeksforgeeks,Best’, ‘Geeksforgeeks@Best’]
Method 2 : Using join() and list comprehension
Similar to above method, difference being task of joining is performed using join(), rather than + operator.
Example:
Python3
test_str1 = 'Geeksforgeeks'
test_str2 = "Best"
print ( "The original string 1 is : " + str (test_str1))
print ( "The original string 2 is : " + str (test_str2))
join_list = [ "+" , "*" , "-" , "$" , "," , "@" ]
res = [delim.join([test_str1, test_str2]) for delim in join_list]
print ( "All delimiters concatenations : " + str (res))
|
Output:
The original string 1 is : Geeksforgeeks
The original string 2 is : Best
All delimiters concatenations : [‘Geeksforgeeks+Best’, ‘Geeksforgeeks*Best’, ‘Geeksforgeeks-Best’, ‘Geeksforgeeks$Best’, ‘Geeksforgeeks,Best’, ‘Geeksforgeeks@Best’]
The time and space complexity for all the methods are the same:
Time Complexity: O(n)
Space Complexity: O(n)
Method 3 : Using the format() method
This approach involves the use of the format() method which allows you to format the string.
Python3
test_str1 = 'Geeksforgeeks'
test_str2 = "Best"
print ( "The original string 1 is : " + str (test_str1))
print ( "The original string 2 is : " + str (test_str2))
join_list = [ "+" , "*" , "-" , "$" , "," , "@" ]
res = [f "{test_str1}{delim}{test_str2}" for delim in join_list]
print ( "All delimiters concatenations : " + str (res))
|
Output
The original string 1 is : Geeksforgeeks
The original string 2 is : Best
All delimiters concatenations : ['Geeksforgeeks+Best', 'Geeksforgeeks*Best', 'Geeksforgeeks-Best', 'Geeksforgeeks$Best', 'Geeksforgeeks,Best', 'Geeksforgeeks@Best']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 4 : using a loop and string concatenation
step-by-step breakdown of the program:
- Initialize two string variables test_str1 and test_str2 with values “Geeksforgeeks” and “Best” respectively.
- Initialize a list variable join_list with values [“+”, “*”, “-“, “$”, “,”, “@”] which are the delimiters to use for concatenating the two strings.
- Initialize an empty list res that will hold the concatenated strings with each delimiter.
- Start a loop that will iterate through each delimiter in the join_list list.
- Within the loop, concatenate test_str1, the current delimiter, and test_str2 together into a single string, using the + operator.
- Append the resulting string to the res list.
- After the loop has finished, print the res list, with a message “All delimiters concatenations : ” concatenated with the string representation of the list.
- End of program.
Python3
test_str1 = 'Geeksforgeeks'
test_str2 = "Best"
join_list = [ "+" , "*" , "-" , "$" , "," , "@" ]
res = []
for delim in join_list:
res.append(test_str1 + delim + test_str2)
print ( "All delimiters concatenations : " + str (res))
|
Output
All delimiters concatenations : ['Geeksforgeeks+Best', 'Geeksforgeeks*Best', 'Geeksforgeeks-Best', 'Geeksforgeeks$Best', 'Geeksforgeeks,Best', 'Geeksforgeeks@Best']
The time complexity of this approach is O(n), where n is the number of delimiters.
The auxiliary space complexity is also O(n), since the result list res grows with the number of delimiters.
Share your thoughts in the comments
Please Login to comment...