strip()函數的作用是去除字符串中開頭或者結尾中所包含的指定字符,然後返回一個結果,但是原來字符串的內容並不會改變。
如果只是刪除單個指定的字符應該很好理解,如果刪除多個指定的字符,會得到一種什麼樣的結果呢?
這裏我們以去除字符串結尾部門的指定字符爲例,源代碼如下
print "This is the test of 18"
someStr = "Hi!...!......!"
print "The results of. is:",someStr.strip(".") (a)
print "The string is ", someStr (b)
print "The results of! is:",someStr.strip("!") (c)
print "The string is ", someStr (d)
print "The results of.! is:",someStr.strip(".!") (e)
print "The string is ", someStr (f)
程序輸出的結果如下:
This is the test of 18
The results of. is: Hi!...!......! (a)的輸出結果,因爲開頭或者結尾部分並沒有包含".",所以沒有任何字符刪除
The string is Hi!...!......! (b)的輸出結果,原來的字符串沒有任何變化
The results of! is: Hi!...!...... (c)的輸出結果,因爲這個字符串是以"!"結尾的,所以"!"被刪除
The string is Hi!...!......! (d)的輸出結果,原來的字符串沒有任何變化
The results of.! is: Hi (e)的輸出結果,從這裏可以看到如果刪除多個字符的話,字符串開頭或者結尾部分關於指定字符的任意非空組合都被刪除
The string is Hi!...!......! (f)的輸出結果,原來的字符串沒有任何變化
Press any key to continue . . .