Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3: str.join() with seperator

I have some code which is essentially this:

data = ["some", "data", "lots", "of", "strings"]
separator = "."

output_string = ""
for datum in data:
    output_string += datum + separator

How can I do this with str.join() or a similar built-in function? (or is it not possible?)

like image 365
Leonora Tindall Avatar asked Oct 21 '15 22:10

Leonora Tindall


People also ask

How do you add a separator to a string in Python?

split() will split your string on all available separators, which is also the default behavior when maxsplit isn't set.


1 Answers

If the separator is a variable you can just use variable.join(iterable):

data = ["some", "data", "lots", "of", "strings"]
separator = "."


print(separator.join(data))
some.data.lots.of.strings
like image 91
Padraic Cunningham Avatar answered Sep 25 '22 23:09

Padraic Cunningham