下面的代码:lstrip正在删除一个角色,我不会期望它
s = "www.wired.com"
print s
s = s.lstrip('www.')
print s
输出:
www.wired.com
ired.com
注意在第二行缺少w
。我不确定我是否理解这种行为。我希望:
www.wired.com
wired.com
编辑:
继前两个答案,我现在明白了行为。我的问题是现在:我怎么舍去开头的www.
而不触及其余?
下面的代码:lstrip正在删除一个角色,我不会期望它
s = "www.wired.com"
print s
s = s.lstrip('www.')
print s
输出:
www.wired.com
ired.com
注意在第二行缺少w
。我不确定我是否理解这种行为。我希望:
www.wired.com
wired.com
编辑:
继前两个答案,我现在明白了行为。我的问题是现在:我怎么舍去开头的www.
而不触及其余?
的参数string.lstrip
是字符的列表:
>>> help(string.lstrip)
Help on function lstrip in module string:
lstrip(s, chars=None)
lstrip(s [,chars]) -> string
Return a copy of the string s with leading whitespace removed.
If chars is given and not None, remove characters in chars instead.
>>>
它消除那些主要人物的所有事件。
print s.lstrip('w.') # does the same!
[编辑]:
如果你想滑索初始www.
,但只有当它开始,你可以使用正则表达式或类似的东西:
s = s[4:] if s.startswith('www.') else s
根据文档:
字符参数是字符串指定字符集合被除去...的字符参数不是前缀;相反,它的值的所有组合被剥离
您将实现由只说了相同的结果: '(W')
www.wired.com'.lstrip'
如果你想要更多的东西一般情况下,我会做这样的事情:
i = find(s, 'www.')
if i >= 0:
s = s[0:i] + s[i+4:]
1.您可能想要显示“find”来自的输入语句,并且2.这将从字符串中间移除它。 'string.replace'可以更好地替换/删除任意的事件。 – Gerrat 2014-10-31 01:47:38
删除前导www.
>>> import re
>>> s = "www.wired.com"
>>> re.sub(r'^www\.', '', s)
'wired.com'
+1我现在明白了吧。你将如何去除只有领先的'www'。然后 ? – Juicy 2014-10-31 01:38:33