一聽到去空白
直覺反應就是strip()
結果怎么老得不到我要的結果呢?
就是這么一段句子,
"I just read this in a newspaper"
想試一下算算包含了幾個英文字母,
怎么都去不掉空白啊......
>>> s=s="I just read this in a newspaper"
>>> s.strip()
'I just read this in a newspaper'
再仔細看一下定義
S.strip([chars]) -> str Return a copy of the string S with leading and trailing whitespace removed. If chars is given and not None, remove characters in chars instead.
==>人家都告訴你移除的是自首和字尾的空白了,當然就只移除頭尾處空白了
除了移除空白,他還可以移除別的,不過還是一樣,只限於移除兩邊,跟邊沒關系的不移
看一下例子
>>> s=s="I just read this in a newspaper" >>> s.strip('re') 'I just read this in a newspap' >>> s.strip('er') 'I just read this in a newspap' >>> s.strip('epr') 'I just read this in a newspa'
發現了沒,()內的字,不一定要順序一樣,只要個數一樣且包含的字符也一樣就去掉,不管是re還是er
注意:
(1)當[chars]為空時,就是刪除邊邊的空白符啦(包括'\n', '\r', '\t', ' ')
(2)這里的[chars]刪除序列是只要邊邊(開頭或結尾)上的字符在刪除序列內,就刪除掉。