[Python-ideas] Optional keepsep argument in str.split()

MRAB python at mrabarnett.plus.com
Wed Aug 28 20:57:02 CEST 2013


On 28/08/2013 19:42, Marco Buttu wrote:
> What do you think about an optional `keepsep` argument in str.split(),
> in order to keep the separator?
> Something like the `keepends` of str.splitlines():
>
>       >>> 'I am\ngoing\nto...'.splitlines(keepends=True)
>       ['I am\n', 'going\n', 'to...']
>
> For instance:
>
>       >>> 'python3'.split('n')
>       ['pytho', '3']
>       >>> 'python3'.split('n', keepsep=True)
>       ['python', '3']
>
If it's a _separator_, should it be attached to the previous part?
Shouldn't it be:

 >>> 'python3'.split('n', keepsep=True)
['pytho', 'n', '3']

That might be why the keyword argument of .splitlines method is called
'keepends'.

Usually you're not interested in the separator itself, but only in what
it separates.

What's your use-case?


More information about the Python-ideas mailing list