Python 3: str.join() with seperator

前端 未结 2 1777
滥情空心
滥情空心 2020-12-20 07:14

I have some code which is essentially this:

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

output_string = \"\"
for datum in d         


        
相关标签:
2条回答
  • 2020-12-20 07:25

    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
    
    0 讨论(0)
  • 2020-12-20 07:50
    output_string = ".".join(data)
    

    if you have integers or non-strings in data, then

    output_string = ".".join( str(x) for x in data )
    
    0 讨论(0)
提交回复
热议问题