从 Python 中的字符串中删除前缀和后缀

在 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.removeprefixPythonstr.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.

main.py
def removeprefix(string, prefix): if string.startswith(prefix): return string[len(prefix):] return string
Python indexes are zero-based, so the first character in a string has an index of 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.

main.py
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.

If the string ends with the substring, the function returns a slice of the function up to the specified substring.

我们使用负字符串切片从新字符串切片中排除后缀。

这是完整的代码片段。

主程序
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'