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游戏地图最短路径求解

一.题目要求 参考下图完成游戏地图中从起点到目标点的最短路径寻找问题。   二.设计思路 先对游戏地图做了几个设定,以矩阵来模拟游戏地图。将可行的区域位置赋值0,障碍区赋值为i...

Python代码实现KNN算法

kNN算法是k-近邻算法的简称,主要用来进行分类实践,主要思路如下: 1.存在一个训练数据集,每个数据都有对应的标签,也就是说,我们知道样本集中每一数据和他对应的类别。 2.当输入一个...

python连接oracle数据库实例

本文实例讲述了python连接oracle数据库的方法,分享给大家供大家参考。具体步骤如下: 一、首先下载驱动:(cx_Oracle) http://www.python.net/cre...

利用python模拟实现POST请求提交图片的方法

本文主要给大家介绍的是关于利用python模拟实现POST请求提交图片的方法,分享出来供大家参考学习,下面来一看看详细的介绍: 使用requests来模拟HTTP请求本来是一件非常轻松的...

Django通过dwebsocket实现websocket的例子

与django推荐的channel不同,dwebsocket使用更加方便简单 使用方法1: 只需views.py文件中,将对应的视图函数添加装饰器 accept_websocket-...