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.
Similar Reads
Split a String by a Delimiter in Python
In Python Programming, the split() method is used to split a string into a list of substrings which is based on the specified delimiter. This method takes the delimiter as an argument and returns the list of substrings. Along with this method, we can use various approaches wot split a string by the
2 min read
Remove Multiple Characters from a String in Python
Removing multiple characters from a string in Python can be achieved using various methods, such as str.replace(), regular expressions, or list comprehensions. Each method serves a specific use case, and the choice depends on your requirements. Letâs explore the different ways to achieve this in det
3 min read
Swap elements in String list - Python
Swapping elements in a string list means we need to exchange one element with another throughout the entire string list in Python. This can be done using various methods, such as using replace(), string functions, regular expressions (Regex), etc. For example, consider the original list: ['Gfg', 'is
3 min read
Multiple Indices Replace in String - Python
In this problem, we have to replace the characters in a string at multiple specified indices and the following methods demonstrate how to perform this operation efficiently: Using loop and join()join() is a brute force method where we first convert the string into a list then we iterate through the
3 min read
Python - Disjoint Strings across Lists
Given two lists, extract all the string pairs which are disjoint across i.e. which don't have any character in common. Input : test_list1 = ["haritha", "is", "best"], test_list2 = ["she", "loves", "papaya"] Output : [('haritha', 'loves'), ('is', 'papaya'), ('best', 'papaya')] Explanation : "is" and
4 min read
Python | Delimited String List to String Matrix
Sometimes, while working with Python strings, we can have problem in which we need to convert String list which have strings that are joined by deliminator to String Matrix by separation by deliminator. Lets discuss certain ways in which this task can be performed. Method #1 : Using loop + split() T
5 min read
Create a List of Strings in Python
Creating a list of strings in Python is easy and helps in managing collections of text. For example, if we have names of people in a group, we can store them in a list. We can create a list of strings by using Square Brackets [] . We just need to type the strings inside the brackets and separate the
3 min read
Python - Segregate elements by delimiter
Given a list of Strings, segregate each string by delimiter and output different lists for prefixes and suffixes. Input: test_list = ["7$2", "8$5", "9$1"], delim = "$" Output : ['7', '8', '9'], ['2', '5', '1'] Explanation Different lists for prefixes and suffixes of "$" Input test_list = ["7*2", "8*
6 min read
Lists Of Strings In Python
A list of strings in Python stores multiple strings together. In this article, weâll explore how to create, modify and work with lists of strings using simple examples. Creating a List of StringsWe can use square brackets [] and separate each string with a comma to create a list of strings. [GFGTABS
2 min read
Python - Horizontal Concatenation of Multiline Strings
Horizontal concatenation of multiline strings involves merging corresponding lines from multiple strings side by side using methods like splitlines() and zip(). Tools like itertools.zip_longest() help handle unequal lengths by filling missing values, and list comprehensions format the result. Using
3 min read