這個問題在這里已經有了答案: 了解 python 在字串上的 lstrip 方法 [重復] (3 個回答) 1 小時前關閉。
我的理解是lstrip(arg)根據arg.
我正在執行以下代碼:
'htp://www.abc.com'.lstrip('/')
輸出:
'htp://www.abc.com'
我的理解是,所有字符都應該從左邊剝離,直到/到達。換句話說,輸出應該是:
'www.abc.com'
我也不確定為什么運行以下代碼會生成以下輸出:
'htp://www.abc.com'.lstrip('/:pth')
輸出:
'www.abc.com'
uj5u.com熱心網友回復:
如果您希望給定字串的所有字符都正確,請嘗試 split
url = 'htp://www.abc.com'
print(url.split('//')[1])
輸出
www.abc.com
lstrip 只回傳去除了前導字符的字串的副本,而不是介于兩者之間
uj5u.com熱心網友回復:
我想你想要這個:
a = 'htp://www.abc.com'
a = a[a.find('/') 1:]
來自 Python 檔案:str.lstrip([chars])
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:**
閱讀最后一行,您的疑問將得到解決。
uj5u.com熱心網友回復:
呼叫該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.
其中,顯然意味著開頭(即左側)中的任何空白都將被截斷,或者如果chars指定了引數,則當且僅當字串以任何指定的字符開頭時,它才會洗掉這些字符,即,如果您傳遞'abc'為一個引數,則該字串應該有任何的開始'a','b'或者'c'其他的功能將不會改變任何東西。字串不需要'abc'作為一個整體以 the 開頭。
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
uj5u.com熱心網友回復:
在 Python檔案中,str.lstrip只能洗掉其 args 中指定的前導字符,如果未提供任何字符,則只能洗掉空格。
您可以嘗試這樣使用str.rfind:
>>> url = "https://www.google.com"
>>> url[url.rfind('/') 1:]
'www.google.com'
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/328844.html
下一篇:如何從串列中獲取唯一模式
