strip()函数 去空格\n\r\t函数的用法
strip 同时去掉左右两边的空格
lstrip 去掉左边的空格
rstrip 去掉右边的空格
具体示例如下:
>>>a=" hello world!! "
>>>a.lstrip() 'hello world!! '
>>>a.rstrip() ' hello world!!'
>>>a.strip() 'hello world!!'
声明:s为字符串,rm为要删除的字符序列
s.strip(rm) 删除s字符串中开头、结尾处,位于 rm删除序列的字符
s.lstrip(rm) 删除s字符串中开头处,位于 rm删除序列的字符
s.rstrip(rm) 删除s字符串中结尾处,位于 rm删除序列的字符
注意:
- 当rm为空时,默认删除空白符(包括’\n’, ‘\r’, ‘\t’, ’ ')
>>> a = ' hello world!!'
>>> a.strip()
'hello world!!'
>>> a='\t\thello world!!'
'hello world!!'
>>> a = 'hello world!!\r\n'
>>> a.strip()
'hello world!!'
2.这里的rm删除序列是只要边(开头或结尾)上的字符在删除序列内,就删除掉。
>>> a = '21hello world!!'
>>> a.strip('21')
'hello world!!' 结果是一样的
>>> a.strip('12')
'hello world!!'