Open In App

Python – Join strings by multiple delimiters

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:




# initializing strings
test_str1 = 'Geeksforgeeks'
test_str2 = "Best"
 
# printing original strings
print("The original string 1 is : " + str(test_str1))
print("The original string 2 is : " + str(test_str2))
 
# initializing join list
join_list = ["+", "*", "-", "$", ",", "@"]
 
# + operator used for concatenations
res = [test_str1 + delim + test_str2 for delim in join_list]
 
# printing result
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:




# initializing strings
test_str1 = 'Geeksforgeeks'
test_str2 = "Best"
 
# printing original strings
print("The original string 1 is : " + str(test_str1))
print("The original string 2 is : " + str(test_str2))
 
# initializing join list
join_list = ["+", "*", "-", "$", ",", "@"]
 
# join() operator used for concatenations
res = [delim.join([test_str1, test_str2]) for delim in join_list]
 
# printing result
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.




#initializing strings
test_str1 = 'Geeksforgeeks'
test_str2 = "Best"
 
#printing original strings
print("The original string 1 is : " + str(test_str1))
print("The original string 2 is : " + str(test_str2))
 
#initializing join list
join_list = ["+", "*", "-", "$", ",", "@"]
 
#using format method for concatenations
res = [f"{test_str1}{delim}{test_str2}" for delim in join_list]
 
#printing result
print("All delimiters concatenations : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy

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:

  1. Initialize two string variables test_str1 and test_str2 with values “Geeksforgeeks” and “Best” respectively.
  2. Initialize a list variable join_list with values [“+”, “*”, “-“, “$”, “,”, “@”] which are the delimiters to use for concatenating the two strings.
  3. Initialize an empty list res that will hold the concatenated strings with each delimiter.
  4. Start a loop that will iterate through each delimiter in the join_list list.
  5. Within the loop, concatenate test_str1, the current delimiter, and test_str2 together into a single string, using the + operator.
  6. Append the resulting string to the res list.
  7. After the loop has finished, print the res list, with a message “All delimiters concatenations : ” concatenated with the string representation of the list.
  8. End of program.




# Initializing strings
test_str1 = 'Geeksforgeeks'
test_str2 = "Best"
 
# Initializing join list
join_list = ["+", "*", "-", "$", ",", "@"]
 
# Using loop and string concatenation
res = []
for delim in join_list:
    res.append(test_str1 + delim + test_str2)
 
# Printing result
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.


Article Tags :