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 if-elif-else知识点

有的时候,一个 if … else … 还不够用。比如,根据年龄的划分: 条件1:18岁或以上:adult 条件2:6岁或以上:teenager 条件3:6岁以下:kid Pytho...

Python转码问题的解决方法

比如,若要将某个String对象s从gbk内码转换为UTF-8,可以如下操作 s.decode('gbk').encode('utf-8′) 可是,在实际开发中,我发现,这种办法经常会出...

Python编程实现控制cmd命令行显示颜色的方法示例

Python编程实现控制cmd命令行显示颜色的方法示例

本文实例讲述了Python编程实现控制cmd命令行显示颜色的方法。分享给大家供大家参考,具体如下: 基于win7 + python3.4 运行效果: import ctypes i...

pandas对dataFrame中某一个列的数据进行处理的方法

背景:dataFrame的数据,想对某一个列做逻辑处理,生成新的列,或覆盖原有列的值 下面例子中的df均为pandas.DataFrame()的数据 1、增加新列,或更改某列的值 d...

基于Python pip用国内镜像下载的方法

异常信息: Retrying (Retry(total=2, connect=None, read=None, redirect=None)) after connection br...