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?)
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
output_string = ".".join(data)
如果数据中有整数或非字符串,则
output_string = ".".join( str(x) for x in data )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.