Python中join和split用法实例

yipeiwu_com5年前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.

相关文章

简单的Python2.7编程初学经验总结

简单的Python2.7编程初学经验总结

如果你从来没有使用过Python,我强烈建议你阅读Python introduction,因为你需要知道基本的语法和类型。 包管理 Python世界最棒的地方之一,就是大量的第三方程序包...

Pycharm如何打断点的方法步骤

Pycharm如何打断点的方法步骤

一. python代码的调试方式 1. 使用print语句打印出来 2. 在编辑工具中,加断点跟踪(打断点) 3. 使用日志模块,输出到日志中 下面我们来看一下如何打断点 二. 环境 p...

Python的Flask框架中实现分页功能的教程

Blog Posts的提交 让我们从简单的开始。首页上必须有一张用户提交新的post的表单。 首先我们定义一个单域表单对象(fileapp/forms.py):   cl...

python实现mysql的单引号字符串过滤方法

本文实例讲述了python实现mysql的单引号字符串过滤方法。分享给大家供大家参考,具体如下: 最主要用这个函数,可以处理MySQLdb.escape_string(content)....

解读Django框架中的低层次缓存API

有些时候,对整个经解析的页面进行缓存并不会给你带来太多好处,事实上可能会过犹不及。 比如说,也许你的站点所包含的一个视图依赖几个费时的查询,每隔一段时间结果就会发生变化。 在这种情况下,...