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.

相关文章

Pyramid Mako模板引入helper对象的步骤方法

原理是我们在pyramind的before render event 中插入我们的helper 1. 创建helper.py文件,在里面添加上我们常用的方法 2. 在__init__.p...

python调用百度REST API实现语音识别

目前,语音识别,即将语音内容转换为文字的技术已经比较成熟,遥想当时锤子发布会上展示的讯飞输入法语音识别,着实让讯飞火了一把。由于此类语音识别需要采集大量的样本,才能达到一定的准确度,个人...

Python函数参数操作详解

本文实例讲述了Python函数参数操作。分享给大家供大家参考,具体如下: 简述 在 Python 中,函数的定义非常简单,满足对应的语法格式要求即可。对于调用者来说,只需关注如何传递正确...

python简单的函数定义和用法实例

本文实例讲述了python简单的函数定义和用法。分享给大家供大家参考。具体分析如下: 这里定义了一个温度转换的函数及其用法。 def convertTemp(temp, scale)...

跟老齐学Python之有点简约的元组

关于元组,上一讲中涉及到了这个名词。本讲完整地讲述它。 先看一个例子: >>>#变量引用str >>> s = "abc" >>>...