Issue 28205: Add optional suffix to str.join (original) (raw)

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:37 admin set github: 72392
2016-09-22 11:17:25 steven.daprano set messages: +
2016-09-22 06:35:35 serhiy.storchaka set stage: resolved
2016-09-22 06:25:30 rhettinger set status: open -> closedresolution: rejectedmessages: +
2016-09-21 14:23:35 rhettinger set priority: normal -> lowassignee: rhettingercomponents: + Documentation
2016-09-20 04:54:25 rhettinger set messages: +
2016-09-20 04:53:46 rhettinger set messages: -
2016-09-20 04:09:44 rhettinger set nosy: + rhettingermessages: +
2016-09-19 23:28:05 vstinner set nosy: + vstinnermessages: +
2016-09-19 23:22:51 steven.daprano set messages: +
2016-09-19 20:58:15 barry set nosy: + barrymessages: +
2016-09-19 12:08:07 serhiy.storchaka set nosy: + serhiy.storchakamessages: +
2016-09-19 12:02:32 holdenweb set nosy: + holdenwebmessages: +
2016-09-19 11:51:48 mark.dickinson set nosy: + mark.dickinsonmessages: +
2016-09-19 11:21:37 steven.daprano create