
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Program to Join strings by multiple delimiters
In this article, we will learn how to join strings by multiple delimiters in python.
Methods Used
The following are the various methods to accomplish this task −
Using List Comprehension and “+” Operator
Using List Comprehension and join() Function
Example
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 returns a resultant list using the above methods.
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
When you wish to build a new list based on the values of an existing list, list comprehension provides a shorter/concise syntax.
Algorithm (Steps)
Following are the Algorithms/steps to be followed to perform the desired task –.
Create a variable to store the input string1.
Create another variable to store the input string2.
Print both the input strings.
Initialize a list containing multiple delimiters.
Use the + operator to join both the strings with the input delimiters by traversing through the delimiters list using list comprehension.
Print the resultant list after joining both strings with the input delimiters.
Example
The following program returns a list after joining both the input strings with the input delimiters using list comprehension and “+” Operator –
# input string 1 inputString_1 = 'hello' # input string 2 inputString_2 = "tutorialspoint" # printing input string 1 print("Input String 1: ", inputString_1) # printing input string 2 print("Input String 2: ", inputString_2) # Creating a list containing multiple delimiters delimitersList = ["-", "^", "%", "$", "#"] print("Delimiters List is :", delimitersList) # joining both the strings with the input delimiters using list comprehension # Here + operator is used to concatenate the two input string resultantList = [inputString_1 + d + inputString_2 for d in delimitersList] # printing the resultant string print("Both the strings after joining with the input delimiters:\n", resultantList)
Output
On execution, the above program will generate the following output –
Input String 1: hello Input String 2: tutorialspoint Delimiters List is : ['-', '^', '%', '$', '#'] Both the strings after joining with the input delimiters: ['hello-tutorialspoint', 'hello^tutorialspoint', 'hello%tutorialspoint', 'hello$tutorialspoint', 'hello#tutorialspoint']
Method 2: Using List Comprehension and join() Function
join() function − join() is a string function in Python that is used to join elements of a sequence that are separated by a string separator. This function connects sequence elements to convert to a string.
Algorithm (Steps)
Following are the Algorithm/steps to be followed 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 the list element as delimiters using the join() function.
Print the resultant list after joining both strings with the input delimiters.
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 string 1 inputString_1 = 'hello' # input string 2 inputString_2 = "tutorialspoint" # printing input string 1 print("Input String 1: ", inputString_1) # printing input string 2 print("Input String 2: ", inputString_2) # creating a list containing multiple delimiters delimitersList = ["-", "^", "%", "$", "#"] print("Delimiters List is :", delimitersList) # Join the two lists using the join function where # We passed delimiter as the list element resultantList = [d.join([inputString_1, inputString_2]) for d in delimitersList] # printing the resultant list print("Both the strings after joining with the input delimiters:\n", resultantList)
Output
On execution, the above program will generate the following output –
Input String 1: hello Input String 2: tutorialspoint Delimiters List is : ['-', '^', '%', '$', '#'] Both the strings after joining with the input delimiters: ['hello-tutorialspoint', 'hello^tutorialspoint', 'hello%tutorialspoint', 'hello$tutorialspoint', 'hello#tutorialspoint']
The time and space complexity for all the methods are the same −
Time Complexity − O(n)
Space Complexity − O(n)
Conclusion
In this article, we covered two different techniques for joining strings with multiple delimiters, including the + operator (concatenation operator) and the join() function. Additionally, we learned how to join the list with the delimiter as list elements using list comprehension.
- Related Articles
- How to split strings on multiple delimiters with Python?
- How can I use Python regex to split a string by multiple delimiters?
- Program to reverse words separated by set of delimiters in python
- How we can break a string with multiple delimiters in Python?
- Python program to Sort Strings by Punctuation count
- Python program to sort strings by substring range
- Python Program to compare two strings by ignoring case
- JavaScript filter array by multiple strings?
- Join Strings in Java
- Python Program to Group Strings by K length Using Suffix
- How to join two strings to convert to a single string in Python?
- No Enclosing Delimiters in Python
- Python program to split and join a string?
- How to join multiple arrays in TypeScript?
- Program to generate all possible strings by taking the choices in python
