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常用函数详解

1.函数的介绍 为什么要有函数?因为在平时写代码时,如果没有函数的话,那么将会出现很多重复的代码,这样代码重用率就比较低。。。并且这样的代码维护起来也是很有难度的,为了解决这些问题,就出...

python生成随机密码或随机字符串的方法

本文实例讲述了python生成随机密码或随机字符串的方法。分享给大家供大家参考。具体实现方法如下: import string,random def makePassword(mi...

Python外星人入侵游戏编程完整版

Python外星人入侵游戏编程完整版

PYTHON游戏编程外星人入侵的完整实现思路,具体内容如下 准备工作:下载python,比如Anaconda3(64 bit),导入pygame游戏包 1.外星人设置,alie...

python中seaborn包常用图形使用详解

python中seaborn包常用图形使用详解

seaborn包是对matplotlib的增强版,需要安装matplotlib后才能使用。 所有图形都用plt.show()来显示出来,也可以使用下面的创建画布 fig,ax=plt...

Python微信公众号开发平台

Python微信公众号开发平台

  上大学的时候,对微信公众号开发浅尝辄止的玩了一下,感觉还是挺有意思的。 /post/133677.htm后来服务器到期了,也就搁置了。由于发布web程序,使用PHP很顺手,...