Right-to-left string replace in Python?

后端 未结 7 742
轻奢々
轻奢々 2020-12-01 10:20

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          


        
7条回答
  •  温柔的废话
    2020-12-01 10:32

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

提交回复
热议问题