Python的字符串处理函数strip()

 

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值