I have multiple str-variables and I want to concatenate them to single line but with separator:
var_a + 'SEPARATOR' + var_b + 'SEPARATOR' + var_c + 'SEPARATOR' + var_d
As you can see, it looks disgusting and unefficient.
So, is there any way to make it simpler by adding separator automatically between separate strings?
P.S. I like the way it works in print function:
print(var_a, var_b, var_c, var_d, sep="SEPARATOR")
Use join()
function:
words = [var_a,var_b,var_c,var_d]
my_separator = "this is a separator"
my_expected_sentence= my_separator.join(words)
Update: Also you can use your custom method:
def my_join(args, sep):
sentence=""
for i in args:
sentence+=i+sep
return sentence
words=["abc","def","ghi"]
data = my_join(words,'hello')
print(data)
In this way you can add separator before every words.Like:
def my_preceding_join(args, sep):
sentence=""
for i in args:
sentence+=sep+i
return sentence
data = my_preceding_join(words,'hello')
print(data)