Python中join和split用法实例

yipeiwu_com5年前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 Matplot中文显示完美解决方案

详解Python Matplot中文显示完美解决方案

原因与现象 Matplot是一个功能强大的Python图表绘制库,很遗憾目前版本自带的字体库中并不支持中文字体。所以如果在绘制内容中需要显示中文,那么就会显示为方格字符。 解决办法...

python实现智能语音天气预报

python实现智能语音天气预报

python编写的语音天气预报 本系统主要包括四个函数: 1、获取天气数据 1、输入要查询天气的城市 2、利用urllib模块向中华万年历天气api接口请求天气数据 3、利用gzip解压...

详解Python利用random生成一个列表内的随机数

首先,需要导入random模块: import random 随机取1-33之间的1个随机数,可能重复: random.choice(range(1,34)) print得到...

Python实现判断字符串中包含某个字符的判断函数示例

Python实现判断字符串中包含某个字符的判断函数示例

本文实例讲述了Python实现判断字符串中包含某个字符的判断函数。分享给大家供大家参考,具体如下: #coding=utf8 #参数包含两个: #containVar:查找包含的字符...

使用python实现链表操作

使用python实现链表操作

一、概念梳理 链表是计算机科学里面应用应用最广泛的数据结构之一。它是最简单的数据结构之一,同时也是比较高阶的数据结构(例如棧、环形缓冲和队列) 简单的说,一个列表就是单数据通过索引集合在...