Right-to-left string replace in Python?

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

    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
    

提交回复
热议问题