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

Hi,
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.

2 Likes

you could also double-up the joins:

"!".join("abc").join("!!")
# '!a!b!c!'                 

Or write a helper function:

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

I came here because I wanted to suggest the same extension for str.join. It looks like it is a common task.

I personally would suggest something like a mode.

Using separator="!" and L="abc" in the example below.

  • separator.join(L, mode="inner") (default) give a!b!c as usual
  • separator.join(L, mode="left") give !a!b!c
  • separator.join(L, mode="right") give a!b!c!
  • separator.join(L, mode="outer") give !a!b!c!

In my opinion this would improve the usability and will avoid many custom functions to implement this functionality.

Example for a custom function to add an indent:

def indent(n:int, s:str):
    return f'\n{" "*n}'.join(s.split("\n"), mode="left")

There have been two suggestions in five years. That’s pretty uncommon!

def indent(n:int, s:str):
    return f'\n{" "*n}'.join(s.split("\n"), mode="left")

Why not textwrap.indent?

1 Like

The example for the indentation was just a use case for the mentioned “left” mode and textwrap.indent is preferable.

The reason why I suggested an extension for join with a mode was the creation of a table for a docstring.

+---+---+
|a  |b  |
+===+===+
|100|200|
+---+---+
|20 |30 |
+---+---+

In this example an extended version for join could be useful. I also know about the tabulate package but once in a while some custom code is needed.

You can always prepend and/or append an empty string to the list whose values are being joined without changing how str.join works.

>>> '!'.join(["1", "2"])
'1!2'
>>> '!'.join(["", "1", "2"])
'!1!2'
>>> '!'.join(["", "1", "2", ""])
'!1!2!'

I have seen the examples above and also created my own workaround.

If there is a consensus on the topic, that the existing functionality is sufficient, then that is perfectly fine with me. I just thought that this could be an improvement and would make the world easier for customers. And because this old thread existed, I thought that I am not the only one who would use profit from the suggested change.

Thanks to all of you for your kind replies.