lstrip() 方法如何从左侧删除字符?
How is lstrip() method removing chars from left?
我的理解是lstrip(arg)
根据arg
的值从左边删除字符。
我正在执行以下代码:
'htp://www.abc.com'.lstrip('/')
输出:
'htp://www.abc.com'
我的理解是,所有字符都应该从左边开始删除,直到达到 /
。
换句话说,输出应该是:
'www.abc.com'
我也不确定为什么 运行 以下代码会生成以下输出:
'htp://www.abc.com'.lstrip('/:pth')
输出:
'www.abc.com'
如果您想要给定字符串的所有字符都正确,请尝试拆分
url = 'htp://www.abc.com'
print(url.split('//')[1])
输出
www.abc.com
仅 lstrip returns 去除前导字符的字符串副本,不在两者之间
我想你想要这个:
a = 'htp://www.abc.com'
a = a[a.find('/')+1:]
来自 Python 文档:
str.lstrip([字符数])
Return a copy of the string with leading characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. **The chars argument is not a prefix; rather, all combinations of its values are stripped:**
看完最后一行疑惑就解开了。
调用help
函数显示如下:
Help on built-in function lstrip:
lstrip(chars=None, /) method of builtins.str instance
Return a copy of the string with leading whitespace removed.
If chars is given and not None, remove characters in chars instead.
这显然意味着开头(即左侧)中的任何白色-space 都将被切掉,或者如果指定了 chars
参数,当且仅当该字符串以任何指定的字符开头,即如果您将 'abc'
作为参数传递,则该字符串应以 'a'
、'b'
或 'c'
中的任何一个开头,否则函数将获胜改变什么。
整个字符串不需要以 'abc'
开头。
print(' the left strip'.lstrip()) # strips off the whitespace
the left strip
>>> print('ththe left strip'.lstrip('th')) # strips off the given characters as the string starts with those
e left strip
>>> print('ththe left strip'.lstrip('left')) # removes 't' as 'left' contatins 't' in it
hthe left strip
>>> print('ththe left strip'.lstrip('zeb')) # doesn't change anything as the argument passed doesn't match the beggining of the string
ththe left strip
>>> print('ththe left strip'.lstrip('h')) # doesn't change anything as the argument passed doesn't match the beggining of the string
ththe left strip
在 Python documentation 中,str.lstrip
只能删除其 args 中指定的前导字符,如果没有提供字符,则可以删除空格。
您可以像这样尝试使用 str.rfind
:
>>> url = "https://www.google.com"
>>> url[url.rfind('/')+1:]
'www.google.com'
我的理解是lstrip(arg)
根据arg
的值从左边删除字符。
我正在执行以下代码:
'htp://www.abc.com'.lstrip('/')
输出:
'htp://www.abc.com'
我的理解是,所有字符都应该从左边开始删除,直到达到 /
。
换句话说,输出应该是:
'www.abc.com'
我也不确定为什么 运行 以下代码会生成以下输出:
'htp://www.abc.com'.lstrip('/:pth')
输出:
'www.abc.com'
如果您想要给定字符串的所有字符都正确,请尝试拆分
url = 'htp://www.abc.com'
print(url.split('//')[1])
输出
www.abc.com
仅 lstrip returns 去除前导字符的字符串副本,不在两者之间
我想你想要这个:
a = 'htp://www.abc.com'
a = a[a.find('/')+1:]
来自 Python 文档: str.lstrip([字符数])
Return a copy of the string with leading characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. **The chars argument is not a prefix; rather, all combinations of its values are stripped:**
看完最后一行疑惑就解开了。
调用help
函数显示如下:
Help on built-in function lstrip:
lstrip(chars=None, /) method of builtins.str instance
Return a copy of the string with leading whitespace removed.
If chars is given and not None, remove characters in chars instead.
这显然意味着开头(即左侧)中的任何白色-space 都将被切掉,或者如果指定了 chars
参数,当且仅当该字符串以任何指定的字符开头,即如果您将 'abc'
作为参数传递,则该字符串应以 'a'
、'b'
或 'c'
中的任何一个开头,否则函数将获胜改变什么。
整个字符串不需要以 'abc'
开头。
print(' the left strip'.lstrip()) # strips off the whitespace
the left strip
>>> print('ththe left strip'.lstrip('th')) # strips off the given characters as the string starts with those
e left strip
>>> print('ththe left strip'.lstrip('left')) # removes 't' as 'left' contatins 't' in it
hthe left strip
>>> print('ththe left strip'.lstrip('zeb')) # doesn't change anything as the argument passed doesn't match the beggining of the string
ththe left strip
>>> print('ththe left strip'.lstrip('h')) # doesn't change anything as the argument passed doesn't match the beggining of the string
ththe left strip
在 Python documentation 中,str.lstrip
只能删除其 args 中指定的前导字符,如果没有提供字符,则可以删除空格。
您可以像这样尝试使用 str.rfind
:
>>> url = "https://www.google.com"
>>> url[url.rfind('/')+1:]
'www.google.com'