代码点读机 / Python / removesuffix

removesuffix Python · 字符串方法

如果字符串以指定后缀结尾,就删掉该后缀。

怎么用

字符串.removesuffix(后缀)

小例子

print('hello.txt'.removesuffix('.txt'))

运行结果

hello

提醒:Python 3.9+ 才有。

官方文档:https://docs.python.org/zh-cn/3/library/stdtypes.html#string-methods

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← Python全部词条