Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to Join strings by multiple delimiters
In this article, we will learn how to join strings by multiple delimiters in Python. This technique is useful when you need to create multiple variations of joined strings using different separators.
Methods Used
The following are the various methods to accomplish this task ?
Using List Comprehension and "+" Operator
Using List Comprehension and join() Function
Example Overview
Assume we have taken two input strings and a multiple delimiters list. We will now join both the input strings with the given input delimiters and return a resultant list ?
Input
Input String 1: hello Input String 2: tutorialspoint delimitersList = ["-", "^", "%", "$", "#"]
Output
Both the strings after joining with the input delimiters: ['hello-tutorialspoint', 'hello^tutorialspoint', 'hello%tutorialspoint', 'hello$tutorialspoint', 'hello#tutorialspoint']
In this example, both the input strings are joined with the given multiple delimiters "-", "^", "%", "$", and "#", and a new list is returned.
Method 1: Using List Comprehension and "+" Operator
List comprehension provides a shorter and more concise syntax when you wish to build a new list based on the values of an existing list. The "+" operator concatenates strings directly ?
Algorithm
Following are the steps to perform the desired task ?
Create variables to store the input strings
Initialize a list containing multiple delimiters
Use the + operator to join both the strings with each delimiter using list comprehension
Return the resultant list
Example
The following program returns a list after joining both the input strings with the input delimiters using list comprehension and "+" operator ?
# input strings
inputString_1 = 'hello'
inputString_2 = "tutorialspoint"
# printing input strings
print("Input String 1:", inputString_1)
print("Input String 2:", inputString_2)
# creating a list containing multiple delimiters
delimitersList = ["-", "^", "%", "$", "#"]
print("Delimiters List:", delimitersList)
# joining both strings with delimiters using list comprehension and + operator
resultantList = [inputString_1 + d + inputString_2 for d in delimitersList]
# printing the resultant list
print("Both strings after joining with delimiters:")
print(resultantList)
Input String 1: hello Input String 2: tutorialspoint Delimiters List: ['-', '^', '%', '$', '#'] Both strings after joining with delimiters: ['hello-tutorialspoint', 'hello^tutorialspoint', 'hello%tutorialspoint', 'hello$tutorialspoint', 'hello#tutorialspoint']
Method 2: Using List Comprehension and join() Function
The join() function is a string method that joins elements of a sequence using a specified separator. It's more efficient for joining multiple strings ?
Algorithm
Following are the steps to perform the desired task ?
Initialize a list containing multiple delimiters
Traverse through the delimiters list using list comprehension
Join the given two strings with each delimiter using the
join()functionReturn the resultant list
Example
The following program returns a list after joining both the input strings with the input delimiters using list comprehension and the join() function ?
# input strings
inputString_1 = 'hello'
inputString_2 = "tutorialspoint"
# printing input strings
print("Input String 1:", inputString_1)
print("Input String 2:", inputString_2)
# creating a list containing multiple delimiters
delimitersList = ["-", "^", "%", "$", "#"]
print("Delimiters List:", delimitersList)
# joining strings using join() function with each delimiter
resultantList = [d.join([inputString_1, inputString_2]) for d in delimitersList]
# printing the resultant list
print("Both strings after joining with delimiters:")
print(resultantList)
Input String 1: hello Input String 2: tutorialspoint Delimiters List: ['-', '^', '%', '$', '#'] Both strings after joining with delimiters: ['hello-tutorialspoint', 'hello^tutorialspoint', 'hello%tutorialspoint', 'hello$tutorialspoint', 'hello#tutorialspoint']
Comparison
| Method | Syntax | Best For |
|---|---|---|
| + Operator | string1 + delimiter + string2 |
Simple concatenation |
| join() Function | delimiter.join([string1, string2]) |
Multiple strings or better performance |
Time and Space Complexity
Time Complexity: O(n × m) where n is the number of delimiters and m is the length of strings
Space Complexity: O(n × m) for storing the resultant list
Conclusion
Both methods effectively join strings with multiple delimiters using list comprehension. Use the + operator for simple cases and join() for better performance with longer strings or multiple string joining.
