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

来源:互联网 发布:ubuntu系统软件不见了 编辑:程序博客网 时间:2024/06/15 16:59
# 2.删减strip([chars]),lstrip([chars]),rstrip([chars])(1)strip([chars]) strip()函数族用以去除字符串两端的空白符,保留中间的空白符空白符由string.whitespace常量定义
>>> print ' abc d '.strip().replace(' ','*')abc*dx = '''   line one    line twoand line three'''>>> print x.replace(' ','*')***line*one****line*twoand*line*three****>>> print x.strip().replace(' ','*')line*one****line*twoand*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')abcprint 'abc'.rstrip('c')>>> print 'abc'.rstrip('c')ab


0 0
原创粉丝点击