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画一颗心的方法示例

利用python画一颗心的方法示例

前言 Python一般使用Matplotlib制作统计图形,用它自己的说法是‘让简单的事情简单,让复杂的事情变得可能'。用它可以制作折线图,直方图,条形图,散点图,饼图,谱图等等你能想到...

两个使用Python脚本操作文件的小示例分享

1这是一个创建一个文件,并在控制台写入行到新建的文件中. #!/usr/bin/env python 'makeTextFile.py -- create text file'...

python 同时读取多个文件的例子

Python中打开文本使用的是with语句,比如打开一个文件并读取每一行 with open(filename) as fp: for line in fp: # do...

python画微信表情符的实例代码

python画微信表情符的实例代码

#@project = facepalm #@file = main #@author = Maoliang Ran #@create_time = 2018/8/28 22:...

python判断图片宽度和高度后删除图片的方法

本文实例讲述了python判断图片宽度和高度后删除图片的方法。分享给大家供大家参考。具体分析如下: Image对象有open方法却没有close方法,如果打开图片,判断图片高度和宽度,判...