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 中要将某一类型的变量或者常量转换为字符串对象 ...
随机推荐
- CAP原理的证明
CAP概述 C: Consistency 一致性 A: Availability 可用性 P:Partition Tolerance分区容错性 CAP理论的核心是:一个分布式系统不可能同时很好的满足一 ...
- Webwork 学习之路【05】请求跳转前 xwork.xml 的读取
个人理解 WebWork 与 Struts2 都是将xml配置文件作为 Controler 跳转的基本依据,WebWork 跳转 Action 前 xml 文件的读取依赖 xwork-1.0.jar, ...
- 你应该知道的25道Javascript面试题
题目来自 25 Essential JavaScript Interview Questions.闲来无事,正好切一下. 一 What is a potential pitfall with usin ...
- 青瓷引擎之纯JavaScript打造HTML5游戏第二弹——《跳跃的方块》Part 10(排行榜界面&界面管理)
继上一次介绍了<神奇的六边形>的完整游戏开发流程后(可点击这里查看),这次将为大家介绍另外一款魔性游戏<跳跃的方块>的完整开发流程. (点击图片可进入游戏体验) 因内容太多,为 ...
- Javascript DOM操作实例
最近在学DOM,但是还是没有办法很好的记住API,想找些例子来练习,网上的例子将一个个DOM对象方法挨个举例,并没有集合在一起用,效果不尽人意.所以自己写一份实例,顺便巩固下学到的知识. ...
- Android Intent的几种用法全面总结
Android Intent的几种用法全面总结 Intent, 用法 Intent应该算是Android中特有的东西.你可以在Intent中指定程序要执行的动作(比如:view,edit,dial), ...
- java中的数据结构 --- 集合
集合类在Java.util包中! 在java中常用的是Set,Map,和List. 容器和迭代器 链表 集 映射
- 三大范式与BCNF
引用:http://www.cnblogs.com/ybwang/archive/2010/06/04/1751279.html 参考: 1.范式间的区别 http://www.cnblogs.com ...
- fatal: Not a valid object name: 'master'.
fatal: Not a valid object name: 'master'. the answer is : That is again correct behaviour. Until you ...
- json:There is a cycle in the hierarchy!
在使用JSONObject.fromObject的时候,出现“There is a cycle in the hierarchy”异常. 意思是出现了死循环,也就是Model之间有循环包含关系: 解决 ...