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将时间或时间间隔转为ISO 8601格式方法示例

利用Python将时间或时间间隔转为ISO 8601格式方法示例

前言 大家都知道,Python自带的datetime库提供了将datetime转为ISO 8610格式的函数,但是对于时间间隔(inteval)并没有提供转换的函数,下面我们动手写一个。...

DJango的创建和使用详解(默认数据库sqlite3)

DJango的创建和使用详解(默认数据库sqlite3)

1.安装虚拟环境 虚拟环境是真实python环境的复制版本。 安装虚拟环境的命令: 1)sudo pip install virtualenv #安装虚拟环境 2)sudo pip in...

详解Python locals()的陷阱

在工作中, 有时候会遇到一种情况: 动态地进行变量赋值, 不管是局部变量还是全局变量, 在我们绞尽脑汁的时候, Python已经为我们解决了这个问题. Python的命名空间通过一种字典...

Python排序算法之选择排序定义与用法示例

Python排序算法之选择排序定义与用法示例

本文实例讲述了Python排序算法之选择排序定义与用法。分享给大家供大家参考,具体如下: 选择排序 选择排序比较好理解,好像是在一堆大小不一的球中进行选择(以从小到大,先选最小球为例):...

python添加菜单图文讲解

python添加菜单图文讲解

分享一个基于tkinter的菜单程序添加操作,希望对需要的朋友有帮助。 打开python集成开发环境,使用 from tkinter import Tk from tkinter im...