strip函数解析
声明: s为字符串,rm为要删除的字符序列
>>> s.strip(rm): 删除s字符串中开头、结尾处,位于rm删除序列的字符
>>> s.lstrip(rm): 删除s字符串中开头处,位于rm删除序列的字符
>>> s.rstrip(rm): 删除s字符串中结尾处,位于rm删除序列的字符
例如:
>>> s = "hello,world,hel"
>>> s1 = s.strip("hel")
>>> s1
'o,world,'
1 当rm为空格时,默认删除空白符(包括’\n’,’\r’,’\t’,’ ‘)
>>> s1 = ' a\n\r\tbcd'
>>> s1
' a\n\r\tbcd'
>>> print s1
a
bcd
>>> s1.strip()
'a\n\r\tbcd'
>>> s2 = '\n\t\rabc'
>>> s2.strip()
'abc'
>>> s3 = 'abc\n\t\r'
>>> s3.strip()
'abc'
2 当rm为非空时,只要rm中要删除的字符序列的字符在字符串s的开头或结尾出现,就删除
>>> a = '123abc'
>>> a.strip('21c')
'3ab'
>>> a
'123abc'
>>> a.strip('c21')
'3ab'
>>> a.strip(c21)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'c21' is not defined
>>>
# split函数解析:
split是分隔函数,将字符串分隔成"字符",保存在一个类表中
``` bash
>>> a = "1 2 3 4 5"
>>> a.split()
['1', '2', '3', '4', '5'] //默认不带参数以空格分隔,之所以为双引号的字符,因为实际python没有字符类型的
>>> b = "abc efg hij klm"
>>> b.split()
['abc', 'efg', 'hij', 'klm']
>>>
>>> c = "name=cntsp|age=25|sex=male" //带参数根据实际需求进行分隔
>>> c.split('|')
['name=cntsp', 'age=25', 'sex=male']
>>> c.split('|')[0]
'name=cntsp'
>>> c.split('|')[0].split('=')
['name', 'cntsp']
>>>
>>> d = 'a b c d e f'
>>> d.split('',1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: empty separator
>>> d.split(' ',1)
['a', 'b c d e f']
>>> d.split(' ',3)
['a', 'b', 'c', 'd e f']
>>> d.split(' ',-1)
['a', 'b', 'c', 'd', 'e', 'f']
>>> d.split(' ',-2)
['a', 'b', 'c', 'd', 'e', 'f']
>>> d.split(' ')
['a', 'b', 'c', 'd', 'e', 'f']
>>>