網站首頁 學習教育 IT科技 金融知識 旅遊規劃 生活小知識 家鄉美食 養生小知識 健身運動 美容百科 遊戲知識 綜合知識
當前位置:趣知科普吧 > IT科技 > 

strip函數|python

欄目: IT科技 / 發佈於: / 人氣:6.12K
<link rel="stylesheet" href="https://js.how234.com/third-party/SyntaxHighlighter/shCoreDefault.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><script type="text/javascript"> SyntaxHighlighter.all(); </script>

python strip函數是什麼?一起來看看小編今天的分享吧!

python strip函數

Python strip() 函數只用於移除字元串頭尾指定的字元(默認爲空格或換行符)或字元序列,不能用於刪除中間部分的字元。

常見用法:

s.strip(rm)   刪除s字元串中開頭、結尾處,位於 rm刪除序列的字元;

s.lstrip(rm)  刪除s字元串中開頭處,位於 rm刪除序列的字元;

s.rstrip(rm)  刪除s字元串中結尾處,位於 rm刪除序列的字元;

注意,s爲字元串,rm爲要刪除的字元序列,只能刪除開頭或是結尾的字元或是字元串,不能刪除中間的字元或是字元串。

例如:

>>> a =‘          123'>>> a.strip()‘123'>>> a=‘abc'>>> a.strip()‘abc'>>> a=‘sdff'>>> a.strip()‘sdff'

這裏的rm刪除序列是隻要邊(開頭或結尾)上的字元在刪除序列內,就刪除掉。

>>> a=‘123abc'>>> astrip(‘21')‘3abc'>>> a.strip(‘12')‘3abc'