python中如何用split()函数实现分割字符串?
python中如何用split()函数实现分割字符串?
本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。
1、split()函数介绍
主要是切割字符串,结果返回由字符串元素组成的一个列表;
通过指定分隔符对字符串进行切片,如果参数num有指定值,则分隔num+1个子字符串;
当传入参数时,必须指定分割字符串。当不传递参数时,此时将整个字符串作为列表的一个元素返回。
2、split()函数语法
str.split(str="",num=string.count(str))[n]
3、参数
str:表示为分隔符,默认为空格,但是不能为空('')。
若字符串中没有分隔符,则把整个字符串作为列表的一个元素。
num:表示分割次数。
如果存在参数num,则仅分隔成num+1个子字符串,并且每一个子字符串可以赋给新的变量。
[n]:表示选取第n个分片。
5、使用实例
split()按照单个分隔符分割字符串
str_test='我住长江头,君住长江尾。日日思君不见君,共饮长江水。'
str_test.split(',')
#结果:
['我住长江头','君住长江尾。日日思君不见君','共饮长江水。']
以上就是python中split()函数分割字符串的实现原理及实例,希望能对你有所帮助哟~更多Python学习教程请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
python装饰器管理函数和类的注意点
python装饰器管理函数和类的注意点本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、注意点(1)用装饰器修饰的函数或类主要应用场...详情>>
2023-11-07 21:15:48
python输入数字变成月份
python输入数字变成月份1、思路说明可计算给定区间的时间差,即两者之间共包含几个月。然后由第一个月(开始时间)逐渐累积,最后得到给定时间区...详情>>
2023-11-07 18:13:40
pythonGevent的使用
1、可以通过gevent轻松实现并发同步或异步编程。gevent中使用的主要模式是Greenlet,它是以C扩展模块的形式访问Python的轻量级协程。2、Greenle...详情>>
2023-11-07 17:51:06
python怎么生成xml文件
用python内置的xml.dom可以对xml文件进行解析处理。什么是xml?XML指可扩展标记语言(EXtensibleMarkupLanguage)XML是一种标记语言,很类似HTMLXM...详情>>
2023-11-07 16:53:54热门推荐
技术干货






