Python中join和split用法实例


Posted in Python onApril 14, 2015

join用来连接字符串,split恰好相反,拆分字符串的。
不用多解释,看完代码,其意自现了。

>>>li = ['my','name','is','bob']

>>>' '.join(li)

'my name is bob'

>>>s = '_'.join(li)

>>>s

'my_name_is_bob'

>>>s.split('_')

['my', 'name', 'is', 'bob']

其join和split的英文版解释如下:

join(...)
S.join(sequence) -> string

Return a string which is the concatenation of the strings in the
sequence.  The separator between elements is S.

split(...)
S.split([sep [,maxsplit]]) -> list of strings

Return a list of the words in the string S, using sep as the
delimiter string.  If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are removed
from the result.

Python 相关文章推荐
零基础写python爬虫之urllib2中的两个重要概念:Openers和Handlers
Nov 05 Python
Python验证企业工商注册码
Oct 25 Python
学习python之编写简单乘法口诀表实现代码
Feb 27 Python
Python基于xlrd模块操作Excel的方法示例
Jun 21 Python
python pyinstaller 加载ui路径方法
Jun 10 Python
python关于矩阵重复赋值覆盖问题的解决方法
Jul 19 Python
Python统计文本词汇出现次数的实例代码
Feb 27 Python
Windows下Pycharm远程连接虚拟机中Centos下的Python环境(图文教程详解)
Mar 19 Python
Django用数据库表反向生成models类知识点详解
Mar 25 Python
Django {{ MEDIA_URL }}无法显示图片的解决方式
Apr 07 Python
Selenium关闭INFO:CONSOLE提示的解决
Dec 07 Python
Python&Matlab实现樱花的绘制
Apr 07 Python
Python中__name__的使用实例
Apr 14 #Python
Python中的列表知识点汇总
Apr 14 #Python
Python中使用copy模块实现列表(list)拷贝
Apr 14 #Python
复习Python中的字符串知识点
Apr 14 #Python
Python内置函数dir详解
Apr 14 #Python
Python最基本的数据类型以及对元组的介绍
Apr 14 #Python
Python isinstance函数介绍
Apr 14 #Python
You might like
用PHP+MySQL搭建聊天室功能实例代码
2012/08/20 PHP
PHP读取文件或采集时解决中文乱码
2021/03/09 PHP
jQuery EasyUI API 中文文档 - ValidateBox验证框
2011/10/06 Javascript
js Function类型
2011/12/04 Javascript
六款帮助你实现惊艳视差滚动效果的jQuery插件
2012/09/14 Javascript
批量实现面向对象的实例代码
2013/07/01 Javascript
浅谈jQuery中的事件
2015/03/23 Javascript
javascript制作游戏开发碰撞检测的封装代码
2015/03/31 Javascript
jQuery实现的进度条效果
2015/07/15 Javascript
javascript的BOM汇总
2015/07/16 Javascript
js点击列表文字对应该行显示背景颜色的实现代码
2015/08/05 Javascript
JS基础随笔(菜鸟必看篇)
2016/07/13 Javascript
原生js实现仿window10系统日历效果的实例
2017/10/31 Javascript
详解vue静态资源打包中的坑与解决方案
2018/02/05 Javascript
微信小程序数据统计和错误统计的实现方法
2019/06/26 Javascript
[05:00]TI9战队采访 - Royal Never Give Up
2019/08/20 DOTA
Python增量循环删除MySQL表数据的方法
2016/09/23 Python
关于pip的安装,更新,卸载模块以及使用方法(详解)
2017/05/19 Python
numpy向空的二维数组中添加元素的方法
2018/11/01 Python
Python 互换字典的键值对实例
2019/02/12 Python
pyqt5 使用label控件实时显示时间的实例
2019/06/14 Python
使用Python制作一个打字训练小工具
2019/10/01 Python
Django框架静态文件处理、中间件、上传文件操作实例详解
2020/02/29 Python
Python读取VOC中的xml目标框实例
2020/03/10 Python
如何用Anaconda搭建虚拟环境并创建Django项目
2020/08/02 Python
python statsmodel的使用
2020/12/21 Python
CSS3简单实现照片墙
2014/12/12 HTML / CSS
Canvas 文本转粒子效果的实现代码
2019/02/14 HTML / CSS
请写出char *p与"零值"比较的if语句
2014/09/24 面试题
行政管理专业推荐信
2013/11/02 职场文书
中专毕业生自荐信
2013/11/16 职场文书
文明青少年标兵事迹材料
2014/01/28 职场文书
幼儿园教师获奖感言
2014/03/11 职场文书
上党课的心得体会
2014/09/02 职场文书
小组口号霸气押韵
2015/12/24 职场文书
100句拼搏进取的名言警句,值得一读!
2019/10/07 职场文书