[Python-ideas] str.rreplace

Steven D'Aprano steve at pearwood.info
Sat Jan 25 02:17:25 CET 2014


On Fri, Jan 24, 2014 at 03:33:48PM -0500, random832 at fastmail.us wrote:
> 
> On Fri, Jan 24, 2014, at 14:17, MRAB wrote:
> > On 2014-01-24 16:56, Antoine Pitrou wrote:
> > > On Fri, 24 Jan 2014 08:47:14 -0800 (PST)
> > > Ram Rachum <ram.rachum at gmail.com> wrote:
> > >> I propose implementing str.rreplace. (It'll be to str.replace what
> > >> str.rsplit is to str.split.)
> > >
> > > I suppose it only differs when the count parameter is supplied?
> > >
> > Not necessarily:
> > 
> >  >>> 'aaa'.replace('aa', 'x')
> > 'xa'
> >  >>> 'aaa'.rreplace('aa', 'x')
> > 'ax'

Good catch!

> >>>'aaa'[::-1].replace('aa'[::-1],'x'[::-1])[::-1]
> 'ax'

That is very possibly the ugliest Python code I have ever seen :-)


-- 
Steven


More information about the Python-ideas mailing list