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 逐行分割大txt文件的方法

代码如下所示: # -*- coding: <encoding name> -*- import io LIMIT = 150000 file_count = 0 url...

Python selenium文件上传方法汇总

文件上传是所有UI自动化测试都要面对的一个头疼问题,今天博主在这里给大家分享下自己处理文件上传的经验,希望能够帮助到广大被文件上传坑住的seleniumer。 首先,我们要区分出上传按钮...

解决Tensorflow使用pip安装后没有model目录的问题

解决Tensorflow使用pip安装后没有model目录的问题

在使用pip安装Tensorflow后,在其目录中没有找到model目录,重复安装了两遍依然没有,原因未知。 于是,使用源码安装的方法: (1)收下,使用git clone源码工程: g...

详解Python3之数据指纹MD5校验与对比

MD5消息摘要算法(英语:MD5 Message-Digest Algorithm),一种被广泛使用的密码散列函数,可以产生出一个128位(16字节)的散列值(hash value),用...

python海龟绘图实例教程

本文以实例形式介绍了python turtle模块即海龟绘图的使用方法,对于需要进行图形编程的朋友相信会有一定的借鉴价值。 python turtle模块简介:  python...