
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Join Strings by Multiple Delimiters in Python
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.