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 pillow模块使用方法详解

pillow Pillow是PIL的一个派生分支,但如今已经发展成为比PIL本身更具活力的图像处理库。pillow可以说已经取代了PIL,将其封装成python的库(pip即可安装),...

Python实现在线暴力破解邮箱账号密码功能示例【测试可用】

本文实例讲述了Python实现在线暴力破解邮箱账号密码功能。分享给大家供大家参考,具体如下: dic 字典格式如下(mail.txt) : username@gmail.com:pa...

常见python正则用法的简单实例

下面列出Python正则表达式的几种匹配用法: 1.测试正则表达式是否匹配字符串的全部或部分 regex=ur"" #正则表达式 if re.search(regex, subj...

Python中返回字典键的值的values()方法使用

 values()方法返回给定的字典中所有可用值的列表。 语法 以下是values()方法的语法: dict.values() 参数   &...

Python3实现发送QQ邮件功能(附件)

本文实例为大家分享了Python3实现发送QQ邮件功能:附件,供大家参考,具体内容如下 可以成功发送邮件附件,但是邮件主要内容无法发送,有空再去找找原因 import smtplib...