Sunday, December 29, 2024
Google search engine
HomeLanguagesPython – Join strings by multiple delimiters

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 = ‘GeeksforLazyroar’, test_str2 = “Best”, join_list = [“+”, “*”, “-“, “$”, “,”, “@”]

Output : [‘GeeksforLazyroar+Best’, ‘GeeksforLazyroar*Best’, ‘GeeksforLazyroar-Best’, ‘GeeksforLazyroar$Best’, ‘GeeksforLazyroar,Best’, ‘GeeksforLazyroar@Best’]

Explanation : Elements are concatenated with all desired delimiters.

Input : test_str1 = ‘GeeksforLazyroar’, test_str2 = “Best”, join_list = [“+”, “*”, “-“, “$”]

Output : [‘GeeksforLazyroar+Best’, ‘GeeksforLazyroar*Best’, ‘GeeksforLazyroar-Best’, ‘GeeksforLazyroar$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




# initializing strings
test_str1 = 'GeeksforLazyroar'
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 : GeeksforLazyroar

The original string 2 is : Best

All delimiters concatenations : [‘GeeksforLazyroar+Best’, ‘GeeksforLazyroar*Best’, ‘GeeksforLazyroar-Best’, ‘GeeksforLazyroar$Best’, ‘GeeksforLazyroar,Best’, ‘GeeksforLazyroar@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




# initializing strings
test_str1 = 'GeeksforLazyroar'
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 : GeeksforLazyroar

The original string 2 is : Best

All delimiters concatenations : [‘GeeksforLazyroar+Best’, ‘GeeksforLazyroar*Best’, ‘GeeksforLazyroar-Best’, ‘GeeksforLazyroar$Best’, ‘GeeksforLazyroar,Best’, ‘GeeksforLazyroar@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




#initializing strings
test_str1 = 'GeeksforLazyroar'
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 : GeeksforLazyroar
The original string 2 is : Best
All delimiters concatenations : ['GeeksforLazyroar+Best', 'GeeksforLazyroar*Best', 'GeeksforLazyroar-Best', 'GeeksforLazyroar$Best', 'GeeksforLazyroar,Best', 'GeeksforLazyroar@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 “GeeksforLazyroar” 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.

Python3




# Initializing strings
test_str1 = 'GeeksforLazyroar'
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 : ['GeeksforLazyroar+Best', 'GeeksforLazyroar*Best', 'GeeksforLazyroar-Best', 'GeeksforLazyroar$Best', 'GeeksforLazyroar,Best', 'GeeksforLazyroar@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.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments