# 2.删减 strip([chars]), lstrip([chars]), rstrip([chars]) (1)strip([chars]) strip()函数族用以去除字符串两端的空白符,保留中间的空白符 空白符由string.whitespace常量定义当strip()方法指定输入参数时,删除指定的字符,如:>>> 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
(2)lstrip([chars])和rstrip([chars])分别删除字符串左端和右端的空白符或指定字符。这两个方法只会作用于对应端的指定字符(默认作用于空白符)而对应端不管有多少指定字符都会被删除>>> print 'abc'.strip('a') bc
>>> 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
Python字符串方法详细介绍2_删除
最新推荐文章于 2023-11-16 23:19:06 发布