栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python的split()函数!

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python的split()函数!

split是python内置的一个函数,主要是对字符串进行分割,分隔后的字符串以列表方式返回,是字符串处理方法的重要方法之一。格式:

string.split(separator, number)

其中sepatator为分隔符,常见的有“,”,“/”,“ ”......

number代表分割的次数,为可选参数。一般情况下,我们不使用该参数。

>>> string="math and code"

>>> string.split(" ")

['math', 'and', 'code']

>>> string="math,and,code"

>>> string.split(",")

['math', 'and', 'code']

>>> string="math/and/code"

>>> string.split("/")

['math', 'and', 'code']

上述皆没使用第二个参数

当然,下面使用第二个参数,看看会有什么效果!

>>> string="math and code"

>>> string.split(" ",1)

['math', 'and code']

>>> string.split(" ",2)

['math', 'and', 'code']

当然,有些时候不需要输入任何参数也可分割,默认以空格,tab空格符,回车符等作为分割条件。

>>> string="math and code"#空格

>>> string.split()

['math', 'and', 'code']

>>> string="mathtandtcode"#tab键

>>> print(string)

math    andcode

>>> string.split()

['math', 'and', 'code']

>>> string="mathnandncode"#回车符

>>> print(string)

math

and

code

>>> string.split()

['math', 'and', 'code']

当然,值得注意的一点是字符串使用split方法时,结果会以一个列表的形式返回,原来的对象(字符串)并不会发生改变!这是值得特别关注的地方。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/953776.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号