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?)
split() will split your string on all available separators, which is also the default behavior when maxsplit isn't set.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With