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.

相关文章

matlab中实现矩阵删除一行或一列的方法

实例如下所示: >> A=[1,2,3;4,5,6;7,8,9] A = 1 2 3 4 5 6 7 8 9 删除行: >> A...

python 测试实现方法

 1)doctest 使用doctest是一种类似于命令行尝试的方式,用法很简单,如下 复制代码 代码如下:def f(n): """ >>> f(1) 1...

Python 通过pip安装Django详细介绍

Python 通过pip安装Django详细介绍 经过前面的 Python 包管理工具的学习,接下来我们就要基于前面的知识,来配置 Django 的开发与运行环境。 首先是安装 Djan...

python异步实现定时任务和周期任务的方法

一. 如何调用 def f1(arg1, arg2): print('f1', arg1, arg2) def f2(arg1): print('f2', arg1)...

详解python列表(list)的使用技巧及高级操作

1、合并列表(extend) 跟元组一样,用加号(+)将两个列表加起来即可实现合并: In [1]: x=list(range(1, 13, 2)) In [2]: x + ['b'...