Python中join和split用法实例

yipeiwu_com4年前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 logging日志模块原理及操作解析

一、基本介绍 logging 模块是python自带的一个包,因此在使用的时候,不必安装,只需要import即可。 logging有 5 个不同层次的日志级别,可以将给定的 logg...

Django框架 Pagination分页实现代码实例

一、自定义分页 1、基础版自定义分页 data = [] for i in range(1, 302): tmp = {"id": i, "name": "alex-{}...

修改Python的pyxmpp2中的主循环使其提高性能

引子 之前clubot使用的pyxmpp2的默认mainloop也就是一个poll的主循环,但是clubot上线后资源占用非常厉害,使用strace跟踪发现clubot在不停的poll,...

python文字和unicode/ascll相互转换函数及简单加密解密实现代码

这篇文章主要介绍了python文字和unicode/ascll相互转换函数及简单加密解密实现代码,下面我们来了解一下。 import re import random # ord()...

Python Sleep休眠函数使用简单实例

Python 编程中使用 time 模块可以让程序休眠,具体方法是time.sleep(秒数),其中“秒数”以秒为单位,可以是小数,0.1秒则代表休眠100毫秒。 复制代码 代码如下:...