Python中join和split用法实例

yipeiwu_com6年前Python基础

join用来连接字符串,split恰好相反,拆分字符串的。
不用多解释,看完代码,其意自现了。

复制代码 代码如下:

>>>li = ['my','name','is','bob']
>>>' '.join(li)
'my name is bob'
>>>s = '_'.join(li)
>>>s
'my_name_is_bob'
>>>s.split('_')
['my', 'name', 'is', 'bob']

其join和split的英文版解释如下:

join(...)
S.join(sequence) -> string

Return a string which is the concatenation of the strings in the
sequence.  The separator between elements is S.

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.

相关文章

对numpy中的where方法嵌套使用详解

如同for循环一样,numpy中的where方法可以实现嵌套功能。这是简化嵌套式矩阵逻辑的一个很好的方法。 假设有一个矩阵,需要把小于0的元素改成-1,大于0的元素改成1,而等于0的时候...

安装python3的时候就是输入python3死活没有反应的解决方法

我用brew安装python3 装完了发现 输入python3毫无反应,检查了 $PATH 也没有任何问题 这个时候回去看安装过程,发现安装时有一个错误: ERROR:The `b...

Window 64位下python3.6.2环境搭建图文教程

Window 64位下python3.6.2环境搭建图文教程

python3.6.2环境安装配置图文教程,具体如下 一、需要下载的软件 》python3.6.2.exe (也可以选择更新的版本) ---- -网址 》Anaconda3-4.4.0...

PIL对上传到Django的图片进行处理并保存的实例

1. 介绍 上传的图片文件:如 pic = request.FILES["picture"] # pic是 <class 'django.core.files.uploaded...

Django项目之Elasticsearch搜索引擎的实例

1.使用Docker安装Elasticsearch及其扩展 获取镜像,可以通过网络pull sudo docker image pull delron/elasticsearch-i...