Use the join() method with a separator to get join strings with delimiter in Python. This method returns a string by joining all the elements of an iterable (list, string, tuple), separated by the given separator.
Python joins strings with a delimiter
Simple example code.
# lists
numList = ['1', '2', '3', '4']
s = ', '
print(s.join(numList))
# tuples
numTuple = ('1', '2', '3', '4')
print('#'.join(numTuple))
Output:
If the separator is a variable you can use variable.join(iterable)
:
data = ["some", "data", "lots", "of", "strings"]
separator = "."
print(separator.join(data))
Output: some.data.lots.of.strings
# List of strings
strings = ["Hello", "world", "how", "are", "you"]
# Delimiter
delimiter = " "
# Join the strings with the delimiter
joined_string = delimiter.join(strings)
# Print the result
print(joined_string)
In this example, " ".join(strings)
joins the strings in the list strings
with a space delimiter. You can replace " "
with any other delimiter you want, such as ,
, ;
, |
, etc., depending on your requirements.
Comment if you have any doubts or suggestions on this Python join topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.