Python strip()函数中是否有任何错误?

时间:2018-02-21 11:20:42

标签: python

创建两个字符串:

s1 = "sha1:abcd"
s2 = "sha1:wxyz"

在两个字符串上应用.strip()函数:

s1.strip("sha1:")
>>> 'bcd'

s2.strip("sha1:")
>>> 'wxyz'

我期待以下输出:

s1.strip("sha1:")
>>> 'abcd'

s2.strip("sha1:")
>>> 'wxyz'

我知道strip()函数已被弃用。我很想知道这个问题。我通过官方文档,但没有特别提到":a"或类似的东西。

我也知道其他替代方案,我们可以使用split("sha1:")strip("sha1")后跟strip(":"),提供所需的输出。

3 个答案:

答案 0 :(得分:10)

strip(...)
    S.strip([chars]) -> str

    Return a copy of the string S with leading and trailing
    whitespace removed.
    If chars is given and not None, remove characters in chars instead.

记下字符中的字符

documentation

中详细说明

答案 1 :(得分:1)

它会在字符串的开头和结尾处删除所有字符,例如sha1:

答案 2 :(得分:1)

以下是显示strip实际意图的反例:

s1 = "sha1:abcds"
s2 = "sha1:wxyzs"

print(s1.strip("sha1:"))
print(s2.strip("sha1:"))

输出:

bcd
wxyz

strip()删除了参数中提供的字符,无论是在目标的开头还是结尾都找到它们。