Python中strip方法的妙用

2021-06-22 06:46:03 字數 3433 閱讀 2262

【開胃小菜】

當提到python中strip方法,想必凡接觸過python的同行都知道它主要用來切除空格。有以下兩種方法來實現。

方法一:用內建函式

#if __name__ == '__main__'

:str = ' hello world '

print

'[%s]'

%str.strip()#

方法二:呼叫string模組中方法

#import string

if __name__ == '__main__'

:str = ' hello world '

print

'[%s]'

%string.strip(str)#

不知道大家是否知道這兩種呼叫有什麼區別?以下是個人一些看法

ø  str.strip()是呼叫python的內建函式,string.strip(str)是呼叫string模組中的方法

ø  string.strip(str)是在string模組定義的。而str.strip()是在builtins模組中定義的

問題一:如何檢視乙個模組中方法是否在內建模組有定義?

用dir(模組名)看是否有'__builtins__'屬性。

例如:檢視string模組

# print dir(string) #

問題二、如何檢視python中所有的內建函式

#print dir(sys.modules['__builtin__'])#

問題三、如何檢視內建模組中內建函式定義

# print help(__builtins__)  #

【飯中硬菜】

#if __name__ == '__main__'

:str = 'hello world'

print str.strip('hello'

)print str.strip('hello'

).strip()

print str.strip('heldo'

).strip()    #sentence 1

stt = 'h1h1h2h3h4h'

print stt.strip('h1'

)                #sentence 2

s ='123459947855aaaadgat134f8sfewewrf7787789879879'

print s.strip('0123456789'

)         #sentence 3 #

結果見下頁:

執行結果:

world

world

wor2h3h4

aaaadgat134f8sfewewrf

你答對了嗎?o(∩_∩)o~

如果你都答對了,在此處我奉上32個讚 …

結果分析:

首先我們檢視一下string模組中的strip原始碼:

## strip leading and trailing tabs and spaces

defstrip(s, chars=none):

"""strip(s [,chars]) -> string

return a copy of the string swith leading and trailing

whitespace removed.

if chars is given and not none,remove characters in chars instead.

if chars is unicode, s will beconverted to unicode before stripping.

"""returns.strip(chars)#

冒昧的翻譯一下: 該方法用來去掉首尾的空格和tab。返回乙個去掉空格的s字串的拷貝。如果引數chars不為none有值,那就去掉在chars中出現的所有字元。如果chars是unicode,s在操作之前先轉化為unicode.

下面就上面裡子中的sentence1 \2 \3做個說明:

#str = 'hello world'

print str.strip('heldo'

).strip()#

result:wor

執行步驟:

elloworld

lloworld

oworld

oworl

worl

worwor

具體**執行流程:

#print str.strip('h'

)print str.strip('h'

).strip('e'

)print str.strip('h'

).strip('e'

).strip('l'

)print str.strip('h'

).strip('e'

).strip('l'

).strip('d'

)print str.strip('h'

).strip('e'

).strip('l'

).strip('d'

).strip('o'

)print str.strip('h'

).strip('e'

).strip('l'

).strip('d'

).strip('o'

).strip('l'

)printstr.strip('h'

).strip('e'

).strip('l'

).strip('d'

).strip('o'

).strip('l'

).strip()#

不知道你是否看懂其中的奧妙,我是在專案經理陝奮勇幫助下,一起才發現這個規律。

現在稍微總結一下:

s.strip(chars)使用規則:

首先遍歷chars中的首個字元,看看在s中是否處於首尾位置,如果是就去掉。把去掉後的新字串設定為s,繼續迴圈,從chars中的首個字元開始。如果不在,直接從chars第二個字元開始。一直迴圈到,s中首尾字元都不在chars中,則迴圈終止。

關鍵點:檢視

chars

中字元是否在

s中首尾

看完這個方法發現python原始碼開發人員太牛x了,這麼經典演算法都想的出。

【飯後糕點】

這個方法主要應用於按照特定規則去除兩端的制定字元。如果sentence3就是個很好的應用。

例如: 擷取字串中兩端數字,或者獲取特性字元第一次和最後一次出現之間的字串等等。

關於python中strip方法的理解

1.strip方法並不是去除對稱字串的。比如a 123abcd321 a.strip 方法的結果是abcd 有人可能就把strip方法理解成了 可以去除字串兩端對稱字元 的一種方法。這麼理解是錯誤的。2.strip方法既可以去除字串左側的字元也可以去除字串右側的字元,也可以兩端同時去除。a 123a...

python中 strip 的使用

恰好這兩天用到這個函式,看到網上的介紹都比較簡略,而且表述也不太對。自己試了試,對它有了更深刻的理解。簡介 strip 函式可以移除字串中指定的字元,像這樣 a n t1339jfsiao n t a.strip 1339jfsiao 可以看到當我們不設定strip的引數的時候,預設下該函式刪除了字...

python中strip的用法

python中strip用於移除字串頭尾指定的字元 預設為空格或換行符 或字串行。注意 該方法只能刪除開頭或是結尾的字元,不能刪除中間部分的字元。如下 a i am a student print a.strip i 去除開始的 i am a student print a.strip i tn 去...