Python 3.9.0 新增内置方法:移除前缀和后缀的字符串方法
详情可以点击这里查看
这里简单做下记录
从详情中我们可以看到其具体实现
1
2
3
4
5
6
7
8
9
10
11
12
13
|
def removeprefix(self: str, prefix: str, /) -> str:
if self.startswith(prefix):
return self[len(prefix):]
else:
return self[:]
def removesuffix(self: str, suffix: str, /) -> str:
# suffix='' should not call self[:-0].
if suffix and self.endswith(suffix):
return self[:-len(suffix)]
else:
return self[:]
|
同时列举了几个例子
第一个列子
1
2
3
4
5
|
if test_func_name.startswith("test_"):
print(test_func_name[5:])
else:
print(test_func_name)
|
这里例子如果在Python 3.9.0的话,可以改善为下面的方式
1
2
|
print(test_func_name.removeprefix("test_"))
|
第二个例子
1
2
3
4
5
6
7
|
if funcname.startswith("context."):
self.funcname = funcname.replace("context.", "")
self.contextfunc = True
else:
self.funcname = funcname
self.contextfunc = False
|
如果使用Python 3.9.0的话,可以改善为下面的方式
1
2
3
4
5
6
7
|
if funcname.startswith("context."):
self.funcname = funcname.removeprefix("context.")
self.contextfunc = True
else:
self.funcname = funcname
self.contextfunc = False
|
进一步可以改善为
1
2
3
|
self.contextfunc = funcname.startswith("context.")
self.funcname = funcname.removeprefix("context.")
|
第三个例子
1
2
3
4
5
6
7
|
def strip_quotes(text):
if text.startswith('"'):
text = text[1:]
if text.endswith('"'):
text = text[:-1]
return text
|
如果使用Python 3.9.0的话,可以改善为下面的方式
1
2
3
|
def strip_quotes(text):
return text.removeprefix('"').removesuffix('"')
|
第四个列子
1
2
3
4
5
6
7
|
if name.endswith(('Mixin', 'Tests')):
return name[:-5]
elif name.endswith('Test'):
return name[:-4]
else:
return name
|
如果使用Python 3.9.0的话,可以改善为下面的方式
1
2
3
4
|
return (name.removesuffix('Mixin')
.removesuffix('Tests')
.removesuffix('Test'))
|