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针对mysql数据库的连接、查询、更新、删除操作示例

本文实例讲述了python针对mysql数据库的连接、查询、更新、删除操作。分享给大家供大家参考,具体如下: 连接 一 代码 import pymysql # 打开数据库连接 db...

Python实现Linux中的du命令

Python实现Linux中的du命令

实现代码如下: 说明:现在代码已实现du命令的 -s 统计功能,统计与系统du命令稍有差别,比如用ls -ld /root结果为4096,而du计算出来是1040,用当前python计算...

python利用urllib和urllib2访问http的GET/POST详解

前言 本文主要给大家介绍了关于python如何访问http的GET/POST的相关内容,使用urllib和urllib2,可以轻松实现对http的访问,下面话不多说了,来一起看看详细的介...

PyQt5基本控件使用详解:单选按钮、复选框、下拉框

PyQt5基本控件使用详解:单选按钮、复选框、下拉框

本文主要介绍PyQt5界面最基本使用的单选按钮、复选框、下拉框三种控件的使用方法进行介绍。 1、RadioButton单选按钮/CheckBox复选框。需要知道如何判断单选按钮是否被选中...

Python3.7安装keras和TensorFlow的教程图解

Python3.7安装keras和TensorFlow的教程图解

win10 Python3.7安装keras深度学习集成包 TensorFlow 和Ubuntu下安装keras 在win10下安装 安装时必须检查你的python是否为64位,32位不...