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-图像处理(映射变换)

做计算机视觉方向,除了流行的各种深度学习算法,很多时候也要会基础的图像处理方法。 记录下opencv的一些操作(图像映射变换),日后可以方便使用 先上一张效果图 图二和图三是同一种方法...

对python判断ip是否可达的实例详解

python中使用subprocess来使用shell 关于threading的用法 from __future__ import print_function import sub...

如何关掉pycharm中的python console(图解)

如何关掉pycharm中的python console(图解)

安装好jupyter notebook后,在pycharm中无论运行什么样的python脚本,都会默认使用python的console运行,这种console非常恶心,前几行全是费话。而...

python判断单向链表是否包括环,若包含则计算环入口的节点实例分析

python判断单向链表是否包括环,若包含则计算环入口的节点实例分析

本文实例讲述了python判断单向链表是否包括环,若包含则计算环入口的节点。分享给大家供大家参考,具体如下: 关于数据结构相关的面试题,经常会问到链表中是否存在环结构的判断,下图就是存在...

python3文件复制、延迟文件复制任务的实现方法

python3文件复制、延迟文件复制任务的实现方法

使用python版本3.6.1 工作中测试客户端传输报文速率,写了以下两个脚本。 第一个,简单的复制文件并重命名。 第二个,在循环中增加延时的功能。 使用场景将文件复制并重命名(重...