Python 字符串 removeprefix() 方法
描述
removeprefix() 方法在 Python 3.9 版本中引入。它会删除字符串开头的子串,并返回剩余的字符串。
语法
var.removeprefix(prefix)
参数
- prefix − 要从字符串开头删除的字符串
返回值
removeprefix() 方法返回删除前缀字符串后的字符串。
示例
var = 'Explicit is better than implicit'
var1 = var.removeprefix('Ex')
print ("原始字符串:", var)
print ("删除前缀后:", var1)
var = "Explicit is better than implicit"
var2 = var.removeprefix('Explicit is ')
print ("原始字符串:", var)
print ("删除前缀后:", var2)
var = 'Explicit is better than implicit'
var3 = var.removeprefix("")
print ("原始字符串:", var)
print ("删除前缀后:", var3)
它将产生以下输出−
原始字符串: Explicit is better than implicit
删除前缀后: plicit is better than implicit
原始字符串: Explicit is better than implicit
删除前缀后: better than implicit
原始字符串: Explicit is better than implicit
删除前缀后: Explicit is better than implicit