您的位置:首页 > 编程语言 > Python开发

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

2012-12-28 13:31 337 查看
使用格式:

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: