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

惯例,先看内部帮助文档

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. iframe在ios下无故扩大的问题探究

    移动端页面内嵌了个 iframe,在 ios 下打开却发现页面怪异.比如 demo.代码如下: <!DOCTYPE html> <html lang="zh-CN" ...

  2. 初学git:用git bash往github push代码

    对于我来说,最开始使用github主要是为了使用它的pages功能展示demo.其实这些都是用Github for Windows push上去的,图形化界面的客户端使用确实简单,但是逼格不够,好吧其 ...

  3. 什么是web框架?

    英文原文:http://jeffknupp.com/blog/2014/03/03/what-is-a-web-framework/ 在原文基础上加上了自己在翻译过程中,查看的资料和自己的一些理解,同 ...

  4. 记一次ASP.NET网站的入侵和如何避免被入侵

    ASP.NET网站入侵第二波(LeaRun.信息化快速开发框架 已被笔者拿下) 详细介绍请看第二波 首先我要申明的是不是什么语言写出来的程序就不安全,而是得看写代码的人如何去写这个程序 前些日子我去客 ...

  5. 同态加密-Homomorphic encryption

    同态加密(Homomorphic encryption)是一种加密形式,它允许人们对密文进行特定的代数运算得到仍然是加密的结果,将其解密所得到的结果与对明文进行同样的运算结果一样.换言之,这项技术令人 ...

  6. jquery里面的$(this)和this都什么时候用,有什么区别

    当你用的是jquery时,就用$(this),如果是JS,就用this $(this).html( $(this).html() + " BAM! " + i ); 这个里的htm ...

  7. shell note

    1 输出重定向:ll > aaa 将输出内容 添加到aaa文件中 ll >> aaa将输出内容追加到aaa中 ll &>> abc 将输出内容不论正确或错误都保存 ...

  8. linux 定时执行scrapy命令

    解决方案: 1.以管理员方式执行命令添加计划 sudo crontab -e 添加: 1 0 * * * sh /home/ubuntu/CRON/cron.sh 2. 重启cron服务 sudo s ...

  9. 1019在winddow上面安装MYSQL服务

    -- 在WINDOWS上安装MYSQL,利用运行包直接安装-- 第一步复制文件拷贝到对应目录-- 第二步修改配置文件,创建DATA目录[client]port=3312 [mysql]default- ...

  10. eclipse中的常用插件

    常用插件整理: http://blog.sina.com.cn/s/blog_60b5e13e0102vz6q.html    插件带有链接 http://blog.csdn.net/kagoy/ar ...