How to replace the some characters from the end of a string?

后端 未结 8 1404
迷失自我
迷失自我 2021-02-03 21:34

I want to replace characters at the end of a python string. I have this string:

 s = \"123123\"

I want to replace the last 2 with

8条回答
  •  死守一世寂寞
    2021-02-03 22:27

    When the wanted match is at the end of string, re.sub comes to the rescue.

    >>> import re
    >>> s = "aaa bbb aaa bbb"
    >>> s
    'aaa bbb aaa bbb'
    >>> re.sub('bbb$', 'xxx', s)
    'aaa bbb aaa xxx'
    >>> 
    

提交回复
热议问题