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.

相关文章

python 多维切片之冒号和三个点的用法介绍

python 多维切片之冒号和三个点的用法介绍

初学python和numpy,对在学习多维切片的过程中遇到的问题做个总结。 一维切片就不说了,比较简单,先说下二维的,二维的理解了多维的就简单了。举个例子先建立一个5x5的二维数组 多...

华为校园招聘上机笔试题 扑克牌大小(python)

本文为大家分享了华为校园招聘上机笔试题,供大家参考,具体内容如下 [编程题] 扑克牌大小 时间限制:10秒 空间限制:131072K 扑克牌游戏大家应该都比较熟悉了,一副牌由54张组成,...

浅谈python装饰器探究与参数的领取

首先上原文: 现在,假设我们要增强now()函数的功能,比如,在函数调用前后自动打印日志,但又不希望修改now()函数的定义,这种在代码运行期间动态增加功能的方式,称之为“装饰器”(De...

python根据多个文件名批量查找文件

python根据多个文件名批量查找文件

本文实例为大家分享了python根据多个文件名批量查找文件的具体代码,供大家参考,具体内容如下 老板给了我一个文件列表,让我在一堆文件中挑出来,他要的文件有500多个,一堆文件有上千个,...

详解python运行三种方式

方式一 交互式编程 交互式编程不需要创建脚本文件,是通过 Python 解释器的交互模式进来编写代码。 linux上你只需要在命令行中输入 Python 命令即可启动交互式编程,提示窗口...