现在的位置: 首页 > 综合 > 正文

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

2012年10月14日 ⁄ 综合 ⁄ 共 458字 ⁄ 字号 评论关闭

使用格式:

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

【上篇】
【下篇】

抱歉!评论已关闭.