我有一串琴弦,其中一些琴弦' rec'。我只想删除最后四个字符。
' rec'
换句话说,我有
somestring = 'this is some string rec'
我希望它成为
somestring = 'this is some string'
Python处理此问题的方法是什么?
def rchop(s, suffix): if suffix and s.endswith(suffix): return s[:-len(suffix)] return s somestring = 'this is some string rec' rchop(somestring, ' rec') # returns 'this is some string'