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 . . .