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 Learning 列表的更多操作及示例代码

遍历列表-for循环 列表中存储的元素可能非常多,如果想一个一个的访问列表中的元素,可能是一件十分头疼的事。那有没有什么好的办法呢?当然有!使用 for循环 假如有一个食物名单列表,通过...

Python+OpenCV采集本地摄像头的视频

Python+OpenCV采集本地摄像头的视频

本文实现了用Python和OpenCV配合,调用本地摄像头采集视频,基本上函数的话看opencv的官方文档就Ok了(The OpenCV Reference Manual  R...

python生成指定尺寸缩略图的示例

python生成指定尺寸的缩略图 复制代码 代码如下:def MakeThumb(path, sizes=(75, 32, 16)):    """&nbs...

Flask框架响应、调度方法和蓝图操作实例分析

本文实例讲述了Flask框架响应、调度方法和蓝图操作。分享给大家供大家参考,具体如下: 响应 像现在大部分的互联网应用用的数据传输格式都是JSON,当用户访问一个URL,我们如何通过Fl...

Python设计模式之职责链模式原理与用法实例分析

Python设计模式之职责链模式原理与用法实例分析

本文实例讲述了Python设计模式之职责链模式原理与用法。分享给大家供大家参考,具体如下: 职责链模式(Chain Of Responsibility):使多个对象都有机会处理请求,从而...