Python 字符串 removesuffix() 方法

Python 字符串 removesuffix() 方法

描述

Python 3.9版本中引入了removesuffix()方法。它会从字符串末尾移除指定的子字符串,并返回剩余的字符串。

语法

var.removesuffix(suffix)

参数

  • suffix − 要从字符串末尾移除的字符串

返回值

removesuffix()方法返回移除后缀字符串之后的字符串。

示例

var = 'Explicit is better than implicit.'
var1 = var.removesuffix('.')
print ("原始字符串:", var)
print ("移除后缀:", var1)

var = "Explicit is better than implicit."
var2 = var.removesuffix(' implicit.')
print ("原始字符串:", var)
print ("移除后缀:", var2)

var = 'Explicit is better than implicit.'
var3 = var.removesuffix("")
print ("原始字符串:", var)
print ("移除后缀:", var3)

以上代码将产生如下输出

原始字符串: Explicit is better than implicit.
移除后缀: Explicit is better than implicit
原始字符串: Explicit is better than implicit.
移除后缀: Explicit is better than
原始字符串: Explicit is better than implicit.
移除后缀: Explicit is better than implicit.

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程