[Python-ideas] str.split with padding

Terry Reedy tjreedy at udel.edu
Sat Mar 14 21:23:39 CET 2009

And Clover wrote:
> Here's a simple one I've reinvented in my own apps often enough that it 
> might be worth adding to the built-in split() method:
>     s.split(sep[, maxsplit[, pad]])
> pad, if set True, would pad out the returned list with empty strings 
> (strs/unicodes depending on returned datatype) so that the list was 
> always (maxsplit+1) elements long. This allows one to do things like 
> unpacking assignments:
>     user, hostname= address.split('@', 1, True)
> without having to worry about exceptions when the number of ‘sep’s in 
> the string is unexpectedly fewer than ‘maxsplit’.

I would make pad = <padding string>.  Example use case:

major,minor,micro = pyversion.split('.', 2, '0') # 3.0 = 3.0.0, etc.
# or
major,minor,micro = (int(s) for s in pyversion.split('.', 2, '0') )

I suppose a counter argument is than one could write

Terry Jan Reedy

More information about the Python-ideas mailing list