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
you may reverse a string like so:
myStr[::-1]
to replace just add the .replace
:
print myStr[::-1].replace("iss","XXX",1)
however now your string is backwards, so re-reverse it:
myStr[::-1].replace("iss","XXX",1)[::-1]
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.
myStr[::-1].replace("iss"[::-1],"XXX"[::-1],1)[::-1]