[Python-Dev] backporting string changes to 2.2.3

Guido van Rossum guido@python.org
Fri, 11 Apr 2003 10:27:50 -0400


> On string objects there is a find and rfind, a lstrip and rstrip, but
> there is no rsplit function, is there a reason why there isn't, or is
> this only because nobody implement it ? ( in this case I'll propose a
> patch in a few days). I'm mainly using it for
> 'toto.titi.tata'.rsplit('.',1) -> 'toto.titi','tata' as our internal
> database representation is quite like a logical filesystem.

I think the reason is that there isn't enough need for it.  The
special case of s.rsplit(c, 1) can be coded so easily by using rfind()
that I don't see the need to add it.  Our Swiss Army Knife string type
is beginning to be so loaded with features that I am reluctant to add
more.  The cost of a new feature these days is measured in the number
of books that need to be updated, not the number of lines of code
needed to implement it.

For your amusement only (! :-), I offer this implementation of
rsplit(), which works in Python 2.3:

def rsplit(string, sep, count=-1):
    L = [part[::-1] for part in string[::-1].split(sep[::-1], count)]
    L.reverse()
    return L

--Guido van Rossum (home page: http://www.python.org/~guido/)