Join string before, between, and after

>>> '{1}{0}{1}'.format(s.join(lis), s)
'xaxbxcxdx'

You can use str.replace() to interleave the characters:

>>> lis = ['a','b','c','d']
>>> ''.join(lis).replace('', 'x')
'xaxbxcxdx'

On the other hand, your original solution (or a trivial modification with string formatting) is IMO actually pretty clean and readable.


You can join a list that begins and ends with an empty string:

>>> 'x'.join(['', *lis, ''])
'xaxbxcxdx'

Tags:

Python

String