一个是分割,一个是连接。

惯例,先看内部帮助文档

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函数的更多相关文章

  1. 解释python中join()和split()函数

    join能让我们将指定字符添加至字符串中 a=') print(a) print(type(a)) #1,2,3,4,5,6 #<class 'str'> split()能让我们用指定字符 ...

  2. Python中join 和 split详解(推荐)

    http://www.jb51.net/article/87700.htm python join 和 split方法简单的说是:join用来连接字符串,split恰好相反,拆分字符串的. .join ...

  3. python中join()函数的用法

    join()函数 语法:  'sep'.join(s) 参数说明 sep:分隔符.可以为空 s:要连接的元素序列.字符串.元组.字典 上面的语法即:以sep作为分隔符,将s所有的元素合并成一个新的字符 ...

  4. Python学习之---Python中的内置函数(方法)(更新中。。。)

    add(item)   #将item添加到s中,如果item已经在s中,则无任何效果 break        #退出循环,不会再运行循环中余下的代码 bool()     #将参数转换为布尔型 by ...

  5. Python 函数式编程 & Python中的高阶函数map reduce filter 和sorted

    1. 函数式编程 1)概念 函数式编程是一种编程模型,他将计算机运算看做是数学中函数的计算,并且避免了状态以及变量的概念.wiki 我们知道,对象是面向对象的第一型,那么函数式编程也是一样,函数是函数 ...

  6. Python中的高阶函数与匿名函数

    Python中的高阶函数与匿名函数 高阶函数 高阶函数就是把函数当做参数传递的一种函数.其与C#中的委托有点相似,个人认为. def add(x,y,f): return f( x)+ f( y) p ...

  7. 【转】关于python中re模块split方法的使用

    注:最近在研究文本处理,需要用到正则切割文本,所以收索到了这篇文章,很有用,谢谢原作者. 原址:http://blog.sciencenet.cn/blog-314114-775285.html 关于 ...

  8. python中enumerate()函数用法

    python中enumerate()函数用法 先出一个题目:1.有一 list= [1, 2, 3, 4, 5, 6]  请打印输出:0, 1 1, 2 2, 3 3, 4 4, 5 5, 6 打印输 ...

  9. Python中str()与repr()函数的区别——repr() 的输出追求明确性,除了对象内容,还需要展示出对象的数据类型信息,适合开发和调试阶段使用

    Python中str()与repr()函数的区别 from:https://www.jianshu.com/p/2a41315ca47e 在 Python 中要将某一类型的变量或者常量转换为字符串对象 ...

随机推荐

  1. LINQ基础概述

    介绍LINQ基础之前,首说一下LINQ 的历史和LINQ是什么,然后说一下学习 LINQ要了解的东西和 LINQ基础语法   LINQ 的历史 从语言方面的进化 –委托 –匿名方法 –Lambda表达 ...

  2. 关于BOM

    BOM:浏览器对象模型 (Browser Object Model)主要定义的是JS操作浏览器的方法和属性. 大部分方法都在window下. 常用方法:(JS里面规定如果方法前面是window,win ...

  3. 探究JVM——运行时数据区

    最近在读<深入理解Java虚拟机>,收获颇丰,记录一下,部分内容摘自原书. Java虚拟机在执行Java程序的过程中会把它所管理的内存划分为若干个不同的数据区域.这些区域都有各自的用途,以 ...

  4. 年中review

    1. 做好已知的各种项目,争取能成立固定团队 (项目一期争取能在本月上线)2. 横向扩展技术学习,了解各种技术,加强技术素养(从Server到前端,现在要关注的知识点越来越多,fighting...) ...

  5. 【技巧】“Plugin execution not covered by lifecycle configuration...“异常的处理

    问题现象: 在Eclipse(JEE mars)中新建maven project,选择archetype为:maven-archetype-plugin,结果生成的project存在错误:“Plugi ...

  6. Social Emotional Computing -价值观的运算

    第三节 价值观的运算 第三节  价值观的运算 由于价值观的客观本质就是事物的价值率,因此价值观的运算在客观本质上就是价值率的运算.由于价值观的运算就是为了揭示不同事物价值观之间的函数关系,因此价值观的 ...

  7. ipython又一方便的调试和应用工具!!!

    控制台下://ipython 命令丰富 比如:ls 显示目录  ipython --pylab %run -p *.py quit关闭     示例: In []: %run -p test.py H ...

  8. ViewPager

    1.ViewPager的功能为实现视图滑动 在主布局里边加入: <android.support.v4.view.ViewPager 这个组件,注意这个组件是用来显示左右滑动的界面的,如果不加载 ...

  9. hadoop,mapreduce---分布式计算

    从图中可以看出,map阶段的shuffle: 例如word count,当内存缓冲区满的时候会写到磁盘,一个spill,每个spill,进行分区,排序,最后将同一个分区word合并在一起,写入到磁盘中 ...

  10. Ext-ajax请求数据

    Ext.Ajax.request({ url: webPath+'/news/newsEastmoneyList', method: 'POST', success: function (respon ...