Python:字符串分割

最常用的字符串分割方法莫过于string.split(str)函数

其中str表示依据什么字符进行分割

比如:

s = 'ab=3'
s.split('=')
含义为依据'='为字符串分割

输出的结果为:

['ab','3']
分割完存入列表中,‘=’已经不见了


还有一个函数是将string分割成两半

函数:string.partition(str)

参数str表示依据什么字符串进行分割

输出:一个三元组列表['分隔符左边的字符串','分隔符','分隔符右边的字符串']

比如:

s = 'http://www.chixujohnny.com'
s.partition('://')
print s
输出为:

['http', '://', 'www.chixujohnny.com']


你可能感兴趣的:(Python学习)