Python之split函数的深入理解
作者:mrr 时间:2022-12-22 19:58:25
split函数主要应用场景是Python对字符串的处理中(数据分析,数据处理),以及计算机二级考试的常考基础知识点。
一、split函数的官方定义
定义:Python split()通过指定分隔符对字符串进行切片,如果参数num有指定值,则分隔符num+1个子字符串
语法:split()方法语法:
str.split(str="", num=string.count(str)).
参数:str---分隔符,默认为所有的空字符串,包括空格、换行(\n)、制表符(\t)等
num---分隔次数,默认为-1,即分隔所有
返回值:返回分割后的字符串列表
二、split函数的深刻理解
用下面的方法来记split比较好记。
第一个参数记为分隔符,也就是在这个分隔符的位置才进行分隔
第二个参数记为分隔次数,默认为分隔所有,1的话就分隔一次,即分隔为两个子字符串
str = "Hello#Nice to meet you#qwe#asd#zxc\nYou\nHe"
str1 = str.split("#",1)
#以"#"键为分隔符,分隔1次,分割成2个子字符串
print(str1)
str2 = str.split("#",2)
#以"#"键为分隔符,分隔2次,分割成3个子字符串
print(str2)
str3 = str.split(" ",1)
#以" "键为分隔符,分隔1次,分割成2个子字符串
print(str3)
str4 = str.split(" ",2)
#同理
print(str4)
str5 = str.split()
#未指定分隔符和分隔次数,默认分隔符为空格、换行(\n)、制表符(\t)
print(str5)
str6 = str.split("\n",1)
#同1,2,3,4理
print(str6)
来源:https://blog.csdn.net/qq_61210648/article/details/126687149
标签:Python,split,函数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Golang多线程排序实现快速高效地处理大规模数据
2024-04-27 15:32:29
Python 函数绘图及函数图像微分与积分
2021-07-13 22:53:14
![](https://img.aspxhome.com/file/2023/4/112124_0s.gif)
PhpStorm的使用教程(本地运行PHP+远程开发+快捷键)
2024-05-03 15:13:22
![](https://img.aspxhome.com/file/2023/7/129857_0s.png)
Oracle + mybatis实现对数据的简单增删改查实例代码
2024-01-27 06:14:53
![](https://img.aspxhome.com/file/2023/3/65723_0s.png)
Python生成器定义与简单用法实例分析
2021-03-12 21:45:59
仿淘宝网站的导航标签效果!
2008-11-05 12:37:00
![](https://img.aspxhome.com/file/UploadPic/up/2008110512540282.gif)
用Python实现数据的透视表的方法
2022-10-11 22:16:54
Python3.10的一些新特性原理分析
2023-06-17 06:35:10
![](https://img.aspxhome.com/file/2023/0/134580_0s.jpg)
基于Python3中运算符 **和*的区别说明
2023-12-17 02:22:06
![](https://img.aspxhome.com/file/2023/4/95754_0s.png)
中秋将至利用python画一些月饼从天而降不用买了
2023-08-17 13:08:23
![](https://img.aspxhome.com/file/2023/7/80487_0s.png)
Asp 返回引用类型函数代码
2011-03-10 10:44:00
jQuery实现的简单分页示例
2024-04-09 19:45:29
MySQL安装配置以及安装失败解决过程
2024-01-13 18:55:28
![](https://img.aspxhome.com/file/2023/1/124891_0s.png)
SQLSERVER的非聚集索引结构深度理解
2024-01-20 01:54:00
![](https://img.aspxhome.com/file/2023/1/85601_0s.bmp)
使用VSCODE配置GO语言开发环境的完整步骤
2024-04-27 15:27:51
教你轻松掌握如何保护MySQL中的重要数据
2008-12-19 17:42:00
Python网络编程之xmlrpc模块
2023-04-06 15:19:24
![](https://img.aspxhome.com/file/2023/6/85126_0s.png)
使用Python微信库itchat获得好友和群组已撤回的消息
2021-03-25 08:27:44
![](https://img.aspxhome.com/file/2023/9/134709_0s.png)
关于对Java正则表达式"\\\\"的理解
2023-06-24 07:23:02
![](https://img.aspxhome.com/file/2023/2/60272_0s.png)
python tkinter库的Text记录点击路经和删除记录详情
2021-04-15 03:41:13