Python str.removeprefix() 方法
描述
如果字符串以指定前缀(后缀)开头(结尾),则去掉它;否则返回原字符串。Python 3.9 新增。
语法
str.removeprefix(prefix) / removesuffix(suffix)
参数
| 参数 | 说明 |
|---|---|
prefix / suffix | 要去掉的前缀或后缀 |
返回值
新字符串
实例
print("test_login".removeprefix("test_"))
print("report.csv".removesuffix(".csv"))
print("abc".removeprefix("x"))以上实例输出结果为:
login
report
abc