Finding last occurrence of substring in string, replacing that

前端 未结 7 1671
暗喜
暗喜 2021-01-31 01:07

So I have a long list of strings in the same format, and I want to find the last \".\" character in each one, and replace it with \". - \". I\'ve tried using rfind, but I can\'t

7条回答
  •  甜味超标
    2021-01-31 01:18

    a = "A long string with a . in the middle ending with ."
    

    # if you want to find the index of the last occurrence of any string, In our case we #will find the index of the last occurrence of with

    index = a.rfind("with") 
    

    # the result will be 44, as index starts from 0.

提交回复
热议问题