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 中要将某一类型的变量或者常量转换为字符串对象 ...
随机推荐
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- Laravel 安装多国语言包后,phpstorm 还是报错
问题: 解决办法: vagrant@homestead:~/Code/awbeci$ composer require "overtrue/laravel-lang:~3.0" 总 ...
- 开源Asp.Net Core小型社区系统
源码地址:Github 前言 盼星星盼月亮,Asp.Net Core终于发布啦!! Asp.Net发布时我还在上初中,没有赶上.但是Asp.Net Core我从beta版本便一直关注.最初项目名叫As ...
- js基础知识温习:构造函数与原型
构造函数 构造函数主要用于初始化新对象.按照惯例,构造函数名第一个字母都要大写. 构造函数有别于其它函数在于它使用new操作符来调用生成一个实例对象.换句话说,如果一个函数使用new操作符来调用,则将 ...
- SDRAM读写一字(上)
SDRAM读写一字 系统设计 SDRAM指令 指令 常量名 CKE CSn RAS CASn WEn 备注 空操作 NOP 1 0 1 1 1 行激活 ACTIVE 1 0 0 1 1 读操作 ...
- win10 进入安全模式的方法
[收藏] 楼主 水际天成 只看他 2014-12-19 17:53:26 Windows10出问题了,无法加载了,一直停留在鼠标刚刚出现的那个界面,只能看到计算机屏幕变了颜色,然后就没有任何反映了.想 ...
- linux svn搭建
1 安装: yum install subversion 2 查看svn安装信息: rpm -ql subversion 3 创建svn根目录: svnserve -d -r /svn 4 进入/sv ...
- Criteria查询之sqlRestriction()的理解
sqlRestriction()的理解 在Criteria查询中 使用sqlRestriction()方法来提供SQL语法作限定查询,作为where字句 查看官方给的例子,如下 List cats = ...
- HTML5基础知识(3)--required属性
1.required属性规定在提交之前要填写输入域(不能为空). 2.代码 <body> <form> 账号:<input type="text" r ...
- Android NestedScrolling嵌套滑动机制
Android NestedScrolling嵌套滑动机制 最近项目要用到官网的下拉刷新SwipeRefreshLayout,它是个容器,包裹各种控件实现下拉,不像以前自己要实现事件的拦截,都是通过对 ...