先看一个例子:
>>> ipaddr = "10.122.19.10"
>>> ipaddr.strip()
'10.122.19.10'
>>> ipaddr = '10.122.19.10'
>>> ipaddr.strip()
'10.122.19.10'
>>> ipaddr.split('.')
['10', '122', '19', '10']
>>> ipaddr.strip().split('.')
['10', '122', '19', '10']
python strip()函数 介绍
函数原型
声明:s为字符串,rm为要删除的字符序列
s.strip(rm) 删除s字符串中开头、结尾处,位于 rm删除序列的字符
s.lstrip(rm) 删除s字符串中开头处,位于 rm删除序列的字符
s.rstrip(rm) 删除s字符串中结尾处,位于 rm删除序列的字符
注意:
1. 当rm为空时,默认删除空白符(包括'\n', '\r', '\t', ' ')
例如:
>>> a = ' 123'
>>> a.strip()
'123'
>>> a='\t\tabc'
'abc'
>>> a = 'sdff\r\n'
>>> a.strip()
'sdff'
2.这里的rm删除序列是只要边(开头或结尾)上的字符在删除序列内,就删除掉。
例如 :
复制代码 代码如下:
>>> a = '123abc'
>>> a.strip('21')
'3abc' 结果是一样的
>>> a.strip('12')
Python Split函数的用法总结
说明:
Python中没有字符类型的说法,只有字符串,这里所说的字符就是只包含一个字符的字符串!!!
这里这样写的原因只是为了方便理解,仅此而已。
1.按某一个字符分割,如‘.’
>>> str = ('www.google.com')
>>> print str
www.google.com
>>> str_split= str.split('.')
>>> print str_split
['www', 'google', 'com']
2.按某一个字符分割,且分割n次。如按‘.’分割1次
>>> str_split = str.split('.',1)
>>> print str_split
['www', 'google.com']
3.按某一字符串分割。如:‘||’
>>> str = ('WinXP||Win7||Win8||Win8.1')
>>> str_split = str.split('||')
>>> print str_split
['WinXP', 'Win7', 'Win8', 'Win8.1']
转载于:https://blog.51cto.com/mashengwei/1715789