Full left and right "separator".join(L)

Quite often in the last years, I’ve seen situations in which we would need !a!b!c! as output from a list L = ['a', 'b', 'c'], here with separator='!', but it could be with any separator.

I’ve seen people using:

  1. separator + separator.join(L) + separator

  2. separator.join([""] + L + [""])

Proposal: what do you think about

  • separator.join(L) give a!b!c as usual
  • separator.join(L, prefix=True) give !a!b!c
  • separator.join(L, suffix=True) give a!b!c!
  • separator.join(L, prefix=True, suffix=True) give !a!b!c!

or something similar (with another names) to have cleaner code and avoid the repetition of separator?

It looks pretty niche. In most cases prefix and suffix are different and differ from separator.

I would write

'!%s!' % '!'.join(L)

It is more clear what prefix and suffix are used, and it is easy to change prefix and suffix.

you could also double-up the joins:

# '!a!b!c!'                 

Or write a helper function:

def wrap(inside, outside):
    return inside.join([outside, outside])