簡體   English   中英

與 Python 中的 string.strip([char]) function 有點混淆

[英]Little confused with string.strip([char]) function in Python

我正在嘗試使用 char 參數在 Python 中使用 string.strip([char]) function 。 我以前用它來修剪文本,但使用字符參數,它的行為有點奇怪。 我無法理解其工作背后的邏輯是什么。

string = ' xoxo love xoxo   '

# Leading whitepsace are removed
print(string.strip())
#Result: xoxo love xoxo

print(string.strip(' xoxoe'))
#Result: lov
print(string.strip(' dove '))
#Result: lov

那是因為string.strip([chars])從 left 和 right中刪除了字符集的子集。 這非常重要,因為它不會從整個字符串中刪除這些字符。 如果一個或多個字符子集存在於一側或兩側,它仍然檢查字符串中的下一個字符是否在兩側具有相同順序的子集。

我不知道我是否解釋得很好。

string = ' xoxo love xoxo   '

# Leading whitepsace are removed
print(string.strip())
#Result: xoxo love xoxo

print(string.strip(' xoxoe'))
#Result: lov
print(string.strip(' dove '))
#Result:'xoxo love xoxo'

print(string.strip(' lo '))
#Result:'xoxo love xoxo'

print(string.strip(' xo '))
#Result:'love'

print(string.strip(' xoxol '))
#Result:'ve'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM