Python 2.7.3 (default, Apr 20 2012, 22:44:07)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> s = "www.example.com/help"
>>> s.strip('/')
>>> 'www.example.com/help' #expected 'www.example.comhelp'
>>> t = "/blah/blah/"
>>> t.strip('/')
>>> 'blah/blah' #expected 'blahblah'
>>> s.strip('w.')
>>> 'example.com/help' #expected 'examplecom/help'
>>> f = 'www.example.com'
>>> f.strip('.')
>>> 'www.example.com' #expected 'wwwexamplecom'
>>> f.strip('comw.')
>>> 'example' #as expected
有人可以解释为什么str.strip看起来没有像承诺的那样工作吗?如何从字符串去除正斜杠
从文档:
str.strip([字符])
返回移除了前缘和后的字符串的副本。 chars参数是一个字符串,指定要删除的字符集。如果省略或None,则chars参数默认为删除空白。 chars参数不是前缀或后缀;更确切地说,它的值的所有组合被剥离:
+6
文档说:与*主要*和*删除了尾随*字符。功能完全是这样。它只删除*前导*和*尾部*字符。 –
2012-08-02 10:26:39
+0
它在你引用的文档中说得很对,带去_leading_和_trailing_字符,而不是中间的字符。 –
2012-08-02 10:26:49
+0
对!现在这真是一个愚蠢的问题* |覆盖我的脸| * –
2012-08-02 10:36:11