python如何删除字符串末尾的_spy和 _av
可以使用字符串的切片操作来删除字符串末尾的"_spy"和 "_av"。具体步骤如下:
- 判断字符串末尾是否包含"_spy",如果是则使用切片操作将其删除。
- 判断字符串末尾是否包含"_av",如果是则使用切片操作将其删除。
示例代码如下:
def remove_suffix(string):
if string.endswith("_spy"):
string = string[:-4]
if string.endswith("_av"):
string = string[:-3]
return string
# 测试示例
string1 = "hello_spy"
string2 = "world_av"
string3 = "python"
print(remove_suffix(string1)) # 输出 hello
print(remove_suffix(string2)) # 输出 world
print(remove_suffix(string3)) # 输出 python
运行结果:
hello
world
python
这样就可以删除字符串末尾的"_spy"和 "_av"了
原文地址: https://www.cveoy.top/t/topic/iqJ0 著作权归作者所有。请勿转载和采集!