Right-to-left string replace in Python?

Each Answer to this Q is separated by one/two green lines.

I want to do a string replace in Python, but only do the first instance going from right to left. In an ideal world I’d have:

myStr = "mississippi"
print myStr.rreplace("iss","XXX",1)

> missXXXippi

What’s the best way of doing this, given that rreplace doesn’t exist?

rsplit and join could be used to simulate the effects of an rreplace

>>> 'XXX'.join('mississippi'.rsplit('iss', 1))

>>> myStr[::-1].replace("iss"[::-1], "XXX"[::-1], 1)[::-1]

>>> re.sub(r'(.*)iss',r'\1XXX',myStr)

The regex engine cosumes all the string and then starts backtracking untill iss is found. Then it replaces the found string with the needed pattern.

Some speed tests

The solution with [::-1] turns out to be faster.

The solution with re was only faster for long strings (longer than 1 million symbols).

you may reverse a string like so:


to replace just add the .replace:

print myStr[::-1].replace("iss","XXX",1)

however now your string is backwards, so re-reverse it:


and you’re done.
If your replace strings are static just reverse them in file to reduce overhead.
If not, the same trick will work.


def rreplace(s, old, new):
        place = s.rindex(old)
        return ''.join((s[:place],new,s[place+len(old):]))
    except ValueError:
        return s

You could also use str.rpartition() which splits the string by the specified separator from right and returns a tuple:

myStr = "mississippi"

first, sep, last = myStr.rpartition('iss')
print(first + 'XXX' + last)
# missXXXippi

Using the package fishhook (available through pip), you can add this functionality.

from fishhook import hook

def rreplace(self, old, new, count=-1):
    return self[::-1].replace(old[::-1], new[::-1], count)[::-1]

print('abxycdxyef'.rreplace('xy', '--', count=1))

# 'abxycd--ef'

It’s kind of a dirty hack, but you could reverse the string and replace with also reversed strings.

"mississippi".reverse().replace('iss'.reverse(), 'XXX'.reverse(),1).reverse()

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .