python split分隔字符串之分隔次数

使用格式:

str.split("char",num)

char:表示分隔标识符

num:表示分隔最大次数,为空表示分隔所有

python 示例代码split.py:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
str="www.baidu.com.cn"
strtemp=str.split(".",1)
print strtemp[0]
print strtemp[1]
print strtemp
print str
strtemp=str.split(".",3)
print strtemp[0]
print strtemp[1]
print strtemp[2]
print strtemp[3]
print strtemp
print str

执行split.py,结果如下:

~$ sudo python split.py 
www
baidu.com.cn
['www', 'baidu.com.cn']
www.baidu.com.cn
www
baidu
com
cn
['www', 'baidu', 'com', 'cn']
www.baidu.com.cn