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 bottle框架支持jquery ajax的RESTful风格的PUT和DELETE方法

python bottle框架支持jquery ajax的RESTful风格的PUT和DELETE方法

这两天在用python的bottle框架开发后台管理系统,接口约定使用RESTful风格请求,前端使用jquery ajax与接口进行交互,使用POST与GET请求时都正常,而Reque...

python如何将两个txt文件内容合并

python如何将两个txt文件内容合并

本文实例为大家分享了python将两个txt文件内容合并的具体代码,供大家参考,具体内容如下 分析: 先分别将两个文件中的内容读入列表中,再将列表分割 把不同属性的数据放到单独的列表...

python 实现查找文件并输出满足某一条件的数据项方法

python 实现文件查找和某些项输出 本文是基于给定一文件(students.txt),查找其中GPA分数最高的 输出,同时输出其对应的姓名和学分 一. 思路 首先需要打开文件,读取文...

用Python和WordCloud绘制词云的实现方法(内附让字体清晰的秘笈)

用Python和WordCloud绘制词云的实现方法(内附让字体清晰的秘笈)

环境及模块: Win7 64位 Python 3.6.4 WordCloud 1.5.0 Pillow 5.0.0 Jieba 0.39 目标: 绘制安徽省201...

Python计算三维矢量幅度的方法

本文实例讲述了Python计算三维矢量幅度的方法。分享给大家供大家参考。具体如下: from numpy import * from math import * a=(['x','y...