Python字符串 – removeprefix()函数
在本文中,我们将使用 str.removeprefix(prefix, /)函数删除前缀并返回字符串的其余部分。如果未找到前缀字符串,则返回原始字符串。它是在Python 3.9.0 版本中引入的。
句法:
str.removeprefix(prefix, /)
参数:
suffix- prefix string that we are checking for.
返回值:
返回字符串[len(prefix):] 否则原始字符串的副本。
代码:
示例 1:
Python3
# Python 3.9 code explaining
# str.removeprefix()
s = 'GeeksforGeeks'
# prefix exists
print(s.removeprefix('Geeks'))
print(s.removeprefix('G'))
# whole string is a prefix
# it would print an empty string
print(s.removeprefix('GeeksforGeeks'))
# prefix doesn't exist
# whole string is returned
print(s.removeprefix('for'))
print(s.removeprefix('IT'))
print(s.removeprefix('forGeeks'))
Python3
# Python 3.9 code explaining
# str.removeprefix()
# String for removeprefix()
# If prefix exists then
# remove prefix from the string
# otherwise return original string
string1 = "Welcome to python 3.9"
print("Original String 1 : ", string1)
# prefix exists
result = string1.removeprefix("Welcome")
print("New string : ", result)
string2 = "Welcome Geek"
print("Original String 2 : ", string2)
# prefix doesn't exist
result = string2.removeprefix("Geek")
print("New string : ", result)
输出:
forGeeks
eeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
示例 2:
蟒蛇3
# Python 3.9 code explaining
# str.removeprefix()
# String for removeprefix()
# If prefix exists then
# remove prefix from the string
# otherwise return original string
string1 = "Welcome to python 3.9"
print("Original String 1 : ", string1)
# prefix exists
result = string1.removeprefix("Welcome")
print("New string : ", result)
string2 = "Welcome Geek"
print("Original String 2 : ", string2)
# prefix doesn't exist
result = string2.removeprefix("Geek")
print("New string : ", result)
输出:
Original String 1 : Welcome to python 3.9
New string : to python 3.9
Original String 2 : Welcome Geek
New string : Welcome Geek