rreplace - How to replace the last occurence of an expression in a string

Posted by Barthelemy on Stack Overflow See other posts from Stack Overflow or by Barthelemy
Published on 2010-03-31T20:07:23Z Indexed on 2010/03/31 20:23 UTC
Read the original article Hit count: 132

Filed under:

Is there a quick way in Python to replace strings by starting from the end? For example:

>>> def rreplace(old, new, occurence)
>>>     ... # Code to replace the last occurences of old by new

>>> '<div><div>Hello</div></div>'.rreplace('</div>','</bad>',1)
>>> '<div><div>Hello</div></bad>'

© Stack Overflow or respective owner

Related posts about python