[Python-ideas] str.rreplace

Amber Yust amber.yust at gmail.com
Sat Jan 25 09:01:28 CET 2014


On Fri Jan 24 2014 at 11:55:57 PM, Georg Brandl <g.brandl at gmx.net> wrote:

> There was also the suggestion on stackoverflow of
>
> 'x'.join('aaa'.rsplit('aa', 1))
>
> which might be faster and less colon-y, but is very good at covering up the
> real purpose of the code :)
>

Which is why you throw it in a clearly named function.

def rreplace(haystack, needle, replacement, count):
    """Replace the N rightmost occurrences of one string with another."""
    replacement.join(haystack.rsplit(needle, count))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140125/0382ad37/attachment-0001.html>


More information about the Python-ideas mailing list