在 Python 中从字符串中删除前缀和后缀
Remove the Prefix and Suffix from a String in Python
使用str.removeprefix()
和str.removesuffix()
方法从字符串中删除前缀和后缀。这些方法将前缀和后缀作为参数并将它们从字符串中删除。
# requires Python 3.9+ my_str = '$123apple banana kiwi#456' without_prefix = my_str.removeprefix('$123') print(without_prefix) # 👉️ 'apple banana kiwi#456' without_suffix = my_str.removesuffix('#456') print(without_suffix) # 👉️ '$123apple banana kiwi' without_prefix_and_suffix = my_str.removeprefix('$123').removesuffix('#456') # 👇️ 'apple banana kiwi' print(without_prefix_and_suffix)
str.removeprefix
Pythonstr.removesuffix
版本3.9+
。如果您使用旧版本,请向下滚动到下一个副标题。str.removeprefix
方法检查字符串是否
以指定前缀开头,如果是,则该方法返回不包括前缀的新字符串,否则返回原始字符串的副本。
my_str = '$123apple banana kiwi#456' without_prefix = my_str.removeprefix('$123') print(without_prefix) # 👉️ 'apple banana kiwi#456'
该方法不改变原来的字符串,它返回一个新的字符串。字符串在 Python 中是不可变的。
如果您只需要从字符串中删除后缀,请使用该
str.removesuffix
方法。
my_str = '$123apple banana kiwi#456' without_suffix = my_str.removesuffix('#456') print(without_suffix) # 👉️ '$123apple banana kiwi'
str.removesuffix
方法检查字符串是否
以指定的后缀结尾,如果是,则该方法返回一个不包括后缀的新字符串,否则返回原始字符串的副本。
如果需要从字符串中删除前缀和后缀,请同时调用这两个方法。
my_str = '$123apple banana kiwi#456' without_prefix_and_suffix = my_str.removeprefix( '$123' ).removesuffix('#456') # 👇️ 'apple banana kiwi' print(without_prefix_and_suffix)
从旧 Python 版本的字符串中删除前缀和后缀
如果您使用较旧的 Python 版本(低于 3.9),请按以下方式实现removeprefix
和
removesuffix
函数。
my_str = '$123apple banana kiwi#456' def removeprefix(string, prefix): if string.startswith(prefix): return string[len(prefix):] return string def removesuffix(string, suffix): if string.endswith(suffix): return string[:-len(suffix)] return string without_prefix = removeprefix(my_str, '$123') print(without_prefix) # 👉️ 'apple banana kiwi#456' without_suffix = removesuffix(my_str, '#456') print(without_suffix) # 👉️ '$123apple banana kiwi' without_prefix_and_suffix = removesuffix(without_prefix, '#456') print(without_prefix_and_suffix) # 👉️ 'apple banana kiwi'
该removeprefix
函数检查传入的字符串是否以指定的子字符串开头。
If the string doesn’t start with the specified substring, the function returns
the string as is.
If the string starts with the substring, the function returns a string slice
starting at the character after the last character of the substring.
def removeprefix(string, prefix): if string.startswith(prefix): return string[len(prefix):] return string
0
, and the last character has an index of -1
or len(my_str) - 1
.The string slice starts at the character after the prefix and grabs the rest of
the string.
The removesuffix
function is implemented in a similar way.
def removesuffix(string, suffix): if string.endswith(suffix): return string[:-len(suffix)] return string
If the passed in string doesn’t end with the specified substring, the function
returns the string as is.
我们使用负字符串切片从新字符串切片中排除后缀。
这是完整的代码片段。
my_str = '$123apple banana kiwi#456' def removeprefix(string, prefix): if string.startswith(prefix): return string[len(prefix):] return string def removesuffix(string, suffix): if string.endswith(suffix): return string[:-len(suffix)] return string without_prefix = removeprefix(my_str, '$123') print(without_prefix) # 👉️ 'apple banana kiwi#456' without_suffix = removesuffix(my_str, '#456') print(without_suffix) # 👉️ '$123apple banana kiwi' without_prefix_and_suffix = removesuffix(without_prefix, '#456') print(without_prefix_and_suffix) # 👉️ 'apple banana kiwi'