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 Selenium实现付费音乐批量下载的实现方法

python Selenium实现付费音乐批量下载的实现方法

必备环境 废话 每年回家都要帮我爸下些音乐,这对我来说都是轻车熟路!可当我打开网易云点击下载按钮的时候,可惜已物是人非啦! 开个 VIP 其实也不贵,临时用用也就¥15!但 IT...

在Django的模板中使用认证数据的方法

当前登入的用户以及他(她)的权限可以通过 RequestContext 在模板的context中使用。 注意 从技术上来说,只有当你使用了 RequestContext这些变量才可用。...

查看django执行的sql语句及消耗时间的两种方法

下面介绍两种查看django 执行的sql语句的方法。 方法一: queryset = Apple.objects.all() print queryset.query SELEC...

解决python3 HTMLTestRunner测试报告中文乱码的问题

使用HTMLTestRunner输出的测试报告中,标题和错误说明的中文乱码。 环境: python v3.6 HTMLTestRunner v0.8.2 定位问题 刚开始以为是pytho...

给Python入门者的一些编程建议

Python是一种非常富有表现力的语言。它为我们提供了一个庞大的标准库和许多内置模块,帮助我们快速完成工作。然而,许多人可能会迷失在它提供的功能中,不能充分利用标准库,过度重视单行脚本,...