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使用itertools模块实现排列组合功能示例

本文实例讲述了Python使用itertools模块实现排列组合功能。分享给大家供大家参考,具体如下: 一、笛卡尔积:itertools.product(*iterables[, rep...

python分块读取大数据,避免内存不足的方法

如下所示: def read_data(file_name): ''' file_name:文件地址 ''' inputfile = open(file_name, 'rb'...

python检测lvs real server状态

复制代码 代码如下:import httplibimport osimport time def check_http(i):    try: &...

python实现简单淘宝秒杀功能

本文实例为大家分享了Python淘宝秒杀的具体代码,供大家参考,具体内容如下 昨天茅台在线上搞秒杀,本来想着靠我惊人的手速去秒一瓶,结果。 所以痛定思痛,想想还是用脚本更靠谱。就在网上搜...

MySQL中表的复制以及大型数据表的备份教程

表复制 mysql拷贝表操作我们会常常用到,下面就为您详细介绍几种mysql拷贝表的方式,希望对您学习mysql拷贝表方面能够有所帮助。 假如我们有以下这样一个表: id use...