Python字符串方法详细介绍2_删除

# 2.删减
strip([chars]),
lstrip([chars]),
rstrip([chars])
(1)strip([chars]) strip()函数族用以去除字符串两端的空白符,保留中间的空白符
空白符由string.whitespace常量定义
>>> print ' abc d '.strip().replace(' ','*')
abc*d

x = '''   line one
    line two
and line three
'''
>>> print x.replace(' ','*')
***line*one
****line*two
and*line*three****
>>> print x.strip().replace(' ','*')
line*one
****line*two
and*line*three
当strip()方法指定输入参数时,删除指定的字符,如:
>>> print 'abc'.strip('a')
bc
2)lstrip([chars])和rstrip([chars])分别删除字符串左端和右端的空白符或指定字符。这两个方法只会作用于对应端的指定字符(默认作用于空白符)而对应端不管有多少指定字符都会被删除
>>> print '   abc'.lstrip().replace(' ','*')
abc
>>> print 'abc   '.lstrip().replace(' ','*')
abc***
>>> print 'aabac'.lstrip('a')
bac
>>> print 'abc'.lstrip('c')
abc
>>> print '   abc'.lstrip().replace(' ','*')
abc
>>> print '   abc'.rstrip().replace(' ','*')
***abc
>>> print 'abc'.rstrip('a')
abc
print 'abc'.rstrip('c')
>>> print 'abc'.rstrip('c')
ab


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值