Right-to-left string replace in Python?

后端 未结 7 755
轻奢々
轻奢々 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:45

    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()
    

提交回复
热议问题