什么决定了str.strip()是否可以工作?
来自Python Docs page对于str.strip(),你会发现这个例子。什么决定了str.strip()是否可以工作?
>>> 'www.example.com'.strip('cmowz.') 'example' 
这很好,很好;但为什么这两个没有做任何事情?
>>> 'www.example.com'.strip('.') 'www.example.com' 
>>> 'www.example.com'.strip('co') 
'www.example.com' 
回答:
str.strip()条从端部,所以只有前缘和后的字符被删除。
这两个示例都没有.或c或o两端。
在文档示例中,文本以w开头,该文字位于要删除的字符集中。删除www后,下一个字符.也位于要删除的字符集中。 e不是,剥离停在那里。在示例文本的末尾,首先是m,然后是o,然后是c,最后是.以删除。
从一个字符串中删除特定字符,请使用str.replace()(单个字符),正则表达式(一组),或使用str.translate():
>>> 'www.example.com'.replace('.', '') 'wwwexamplecom' 
>>> 'www.example.com'.translate(None, 'co') 
'www.example.m' 
>>> import re 
>>> re.sub(r'[co]', '', 'www.example.com') 
'www.example.m' 
以上是 什么决定了str.strip()是否可以工作? 的全部内容, 来源链接: utcz.com/qa/259214.html






