python中join和split函数
一个是分割,一个是连接。
惯例,先看内部帮助文档
Help on method_descriptor: join(...)
S.join(iterable) -> string Return a string which is the concatenation of the strings in the
iterable. The separator between elements is S.
(END)
将可迭代对象(包含的应该是str类型的,不然会报错)连接起来, 返回值是str,用法如下:
In [2]: s = ['hello', 'world'] In [3]: '_'.join(s)
Out[3]: 'hello_world' In [4]: ''.join(s)
Out[4]: 'helloworld' In [5]: '&&'.join(s)
Out[5]: 'hello&&world' In [6]: '_'.join((1, 2, 3))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-48e56abfc814> in <module>()
----> 1 '_'.join((1, 2, 3)) TypeError: sequence item 0: expected string, int found
再看split函数:
Help on method_descriptor: 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.
(END)
将字符串分割,空格或者空字符都会被移除,返回值是str的列表,第二个参数是分割次数,用法如下:
In [9]: s = 'life is short, I use Python' In [10]: s.spl
s.split s.splitlines In [10]: s.split()
Out[10]: ['life', 'is', 'short,', 'I', 'use', 'Python'] In [11]: s.split(',')
Out[11]: ['life is short', ' I use Python'] In [12]: s.split(',' or '.')
Out[12]: ['life is short', ' I use Python'] In [13]: s.split(',', 3)
Out[13]: ['life is short', ' I use Python'] In [14]: s.split(',', 5)
Out[14]: ['life is short', ' I use Python'] In [15]: s = 'hello, world. Life is short, I use Python' In [16]: s.split(',', 5)
Out[16]: ['hello', ' world. Life is short', ' I use Python'] In [17]: s.split(',', 2)
Out[17]: ['hello', ' world. Life is short', ' I use Python'] In [18]: s.split(',', 1)
Out[18]: ['hello', ' world. Life is short, I use Python'] In [19]: s.split(',' or '.', 2)
Out[19]: ['hello', ' world. Life is short', ' I use Python']
python中join和split函数的更多相关文章
- 解释python中join()和split()函数
join能让我们将指定字符添加至字符串中 a=') print(a) print(type(a)) #1,2,3,4,5,6 #<class 'str'> split()能让我们用指定字符 ...
- Python中join 和 split详解(推荐)
http://www.jb51.net/article/87700.htm python join 和 split方法简单的说是:join用来连接字符串,split恰好相反,拆分字符串的. .join ...
- python中join()函数的用法
join()函数 语法: 'sep'.join(s) 参数说明 sep:分隔符.可以为空 s:要连接的元素序列.字符串.元组.字典 上面的语法即:以sep作为分隔符,将s所有的元素合并成一个新的字符 ...
- Python学习之---Python中的内置函数(方法)(更新中。。。)
add(item) #将item添加到s中,如果item已经在s中,则无任何效果 break #退出循环,不会再运行循环中余下的代码 bool() #将参数转换为布尔型 by ...
- Python 函数式编程 & Python中的高阶函数map reduce filter 和sorted
1. 函数式编程 1)概念 函数式编程是一种编程模型,他将计算机运算看做是数学中函数的计算,并且避免了状态以及变量的概念.wiki 我们知道,对象是面向对象的第一型,那么函数式编程也是一样,函数是函数 ...
- Python中的高阶函数与匿名函数
Python中的高阶函数与匿名函数 高阶函数 高阶函数就是把函数当做参数传递的一种函数.其与C#中的委托有点相似,个人认为. def add(x,y,f): return f( x)+ f( y) p ...
- 【转】关于python中re模块split方法的使用
注:最近在研究文本处理,需要用到正则切割文本,所以收索到了这篇文章,很有用,谢谢原作者. 原址:http://blog.sciencenet.cn/blog-314114-775285.html 关于 ...
- python中enumerate()函数用法
python中enumerate()函数用法 先出一个题目:1.有一 list= [1, 2, 3, 4, 5, 6] 请打印输出:0, 1 1, 2 2, 3 3, 4 4, 5 5, 6 打印输 ...
- Python中str()与repr()函数的区别——repr() 的输出追求明确性,除了对象内容,还需要展示出对象的数据类型信息,适合开发和调试阶段使用
Python中str()与repr()函数的区别 from:https://www.jianshu.com/p/2a41315ca47e 在 Python 中要将某一类型的变量或者常量转换为字符串对象 ...
随机推荐
- 纯手工打造漂亮的垂直时间轴,使用最简单的HTML+CSS+JQUERY完成100个版本更新记录的华丽转身!
前言 FineUI控件库发展至今已经有 5 个年头,目前论坛注册的QQ会员 5000 多人,捐赠用户 500 多人(捐赠用户转化率达到10%以上,在国内开源领域相信这是一个梦幻数字!也足以证明Fine ...
- mSites and Smarty
目前的页面实现方式是需要向后台请求接口,返回 JSON 数据,动态拼接字符串塞进 DOM 中(innerHTML).考虑用 Smarty 生成静态页面,可以在后台用 PHP 得到数据,字符串拼接,然后 ...
- 移动端打印调试插件 - debug.js 介绍
前文中我们学习过,用 Fiddler 作为代理可以在移动端打开本地的页面进行查看(如何用 fiddler 代理调试本地手机页面),但是对于 js 的调试却无能为力(需要借助其他调试手段,比如 UC浏览 ...
- .NET技术在中国为什么老被人嫌弃
这个话题有点自黑的意思,我从.NET 1.1开始玩.NET,到现在已经11年了,我是看着.NET成长起来,在中国壮大的,也见证了近几年.NET被各种嫌弃,其实说到底还是中国的架构师太少,我是说真正懂行 ...
- 深入理解OOP(第一天):多态和继承(初期绑定和编译时多态)
在本系列中,我们以CodeProject上比较火的OOP系列博客为主,进行OOP深入浅出展现. 无论作为软件设计的高手.或者菜鸟,对于架构设计而言,均需要多次重构.取舍,以有利于整个软件项目的健康构建 ...
- 无法将分支 master 发布到远程 origin,因为远程存储库中已存在具有同一名称的分支
无法将分支 master 发布到远程 origin,因为远程存储库中已存在具有同一名称的分支.发布此分支将导致远程存储库中的分支发生非快进更新. 第一次用oschina的git设置完远程仓库后提交出现 ...
- canvas三角函数做椭圆运动效果
<canvas id="canvas" width="800" height="400" style="background ...
- Servlet从本地文件中读取图片,并显示在页面中
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpSer ...
- jQuery基础--样式篇(3)
1.jQuiery对象与DOM对象 对于刚刚接触jQuery的初学者,我们要清楚认识一点:jQuery对象与DOM对象是不一样的.可能一时半会分不清楚哪些是jQuery对象,哪些是DOM对象,下面重点 ...
- applicationContext.xml和dispatcher-servlet.xml的区别
在SpringMVC项目中我们一般会引入applicationContext.xml和dispatcher-servlet.xml两个配置文件,这两个配置文件具体的区别是什么呢? Spring 官方文 ...