This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add optional suffix to str.join
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: barry, holdenweb, mark.dickinson, rhettinger, serhiy.storchaka, steven.daprano, vstinner
Priority: low Keywords:

Created on 2016-09-19 11:21 by steven.daprano, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (10)
msg276971 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2016-09-19 11:21
It is moderately common to want to join a sequence of substrings with a delimiter rather than a separator, e.g. when joining a sequence of lines into a single string, you usually want a trailing newline as well as newlines between the lines. E.g.:

'\n'.join(['first', 'second', 'third'])


returns 'first\nsecond\nthird' but we usually want a trailing newline as well, but only if the iterable being joined is not empty. If there are no substrings, we don't want to append the delimiter.

Currently the most obvious way to do this is to use a temporary variable:

lines = '\n'.join(substrings)
if lines:
    lines += '\n'
process(lines)

I propose adding a keyword-only argument to str.join(), "suffix", to specify an optional trailing substring added only if the iterable is non-empty. To join lines as above, you would write:

process('\n'.join(substrings, suffix='\n'))

eliminating the unnecessary temporary variable.

Here's a proof of concept:

def join(iterable, sep, *, suffix=None):
    s = sep.join(iterable)
    if s and suffix is not None:
        s += suffix
    return s
msg276973 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2016-09-19 11:51
> Currently the most obvious way to do this is to use a temporary variable:

Or more simply:

    lines = ''.join(substring + '\n' for substring in substrings)
msg276974 - (view) Author: Steve Holden (holdenweb) * (Python committer) Date: 2016-09-19 12:02
If you are going to add such a keyword argument, wouldn't it make sense to maintain compatibility with print, and use end=terminator?
msg276975 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-09-19 12:08
I'm -1. The str interface is already overburdened. This is very special case and there are several ways to do this (Mark's one is the most obvious to me).
msg276985 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2016-09-19 20:58
I'm -1 also, mostly on the grounds that it's not (IME) a very common need and it does clutter up the API.
msg276991 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2016-09-19 23:22
>     lines = ''.join(substring + '\n' for substring in substrings)

Huh. There were three of us looking at this at work yesterday, and none 
of us thought of that.
msg276993 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-09-19 23:28
> '\n'.join(['first', 'second', 'third'])

Hum, the workaround is simple:

lines = ['first', 'second', 'third']
lines.append('')
assert '\n'.join(lines) == 'first\nsecond\nthird\n'
msg277005 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-09-20 04:54
> There were three of us looking at this at work yesterday,
> and none of us thought of that.

How about adding an example to the docs and calling it a day.
msg277196 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-09-22 06:25
Looking again at the comments for the other respondents, I think this should just be closed.  It doesn't make sense to disturb a long standing API or the break the join/split symmetry.
msg277218 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2016-09-22 11:17
> Looking again at the comments for the other respondents, I think this 
> should just be closed.  It doesn't make sense to disturb a long 
> standing API or the break the join/split symmetry.

For what it's worth, in hindsight I agree. I'm a little embarassed that 
I missed such a simple solution. Sorry for the noise!
History
Date User Action Args
2022-04-11 14:58:37adminsetgithub: 72392
2016-09-22 11:17:25steven.dapranosetmessages: + msg277218
2016-09-22 06:35:35serhiy.storchakasetstage: resolved
2016-09-22 06:25:30rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg277196
2016-09-21 14:23:35rhettingersetpriority: normal -> low
assignee: rhettinger
components: + Documentation
2016-09-20 04:54:25rhettingersetmessages: + msg277005
2016-09-20 04:53:46rhettingersetmessages: - msg277004
2016-09-20 04:09:44rhettingersetnosy: + rhettinger
messages: + msg277004
2016-09-19 23:28:05vstinnersetnosy: + vstinner
messages: + msg276993
2016-09-19 23:22:51steven.dapranosetmessages: + msg276991
2016-09-19 20:58:15barrysetnosy: + barry
messages: + msg276985
2016-09-19 12:08:07serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg276975
2016-09-19 12:02:32holdenwebsetnosy: + holdenweb
messages: + msg276974
2016-09-19 11:51:48mark.dickinsonsetnosy: + mark.dickinson
messages: + msg276973
2016-09-19 11:21:37steven.dapranocreate