開心生活站

位置:首頁 > IT科技 > 

python,strip函數

IT科技6.91K
<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'

標籤:Python 函數 strip