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

Masklinn masklinn at masklinn.net
Wed Aug 28 21:36:39 CEST 2013


On 2013-08-28, at 20:57 , MRAB wrote:

> 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']

Which, for what it's worth, is already covered by re.split:

>>> re.split(r"(n)", "python3")
['pytho', 'n', '3']

and the "keeping" split can be handled via findall:

>>> re.findall(r'([^n]+(?:n|$))', "python3")
['python', '3']



More information about the Python-ideas mailing list