在python API中這樣解釋strip()函數:
聲明:s為字符串,rm為要刪除的字符序列
s.strip(rm) 刪除s字符串中開頭、結尾處,位於 rm刪除序列的字符
s.lstrip(rm) 刪除s字符串中開頭處,位於 rm刪除序列的字符
s.rstrip(rm) 刪除s字符串中結尾處,位於 rm刪除序列的字符
注意:
1. 當rm為空時,默認刪除空白符(包括'\n', '\r', '\t', ' ')
例如:
>>> a=' Hello World ' >>> a ' Hello World ' >>> a.strip() 'Hello World' >>> x='\t\r\npython' >>> x '\t\r\npython' >>> x.strip() 'python'
2.rm刪除序列是只要邊(開頭或結尾)上的字符在刪除序列內,就刪除掉。
例如:
>>> aString='123love' >>> aString '123love' >>> aString.strip('12') '3love'