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之wxPython菜单使用详解

python之wxPython菜单使用详解

本文实例讲述了python中wxPython菜单的使用方法,分享给大家供大家参考。具体如下: 先来看看下面这段代码: import wx APP_EXIT=1 #定义一个控件ID...

深入了解python中元类的相关知识

类也是对象 在大多数编程语言中,类就是一组用来描述如何生成一个对象的代码段,在python中也是成立的。 class ObjectCreator: pass my_objec...

使用Python制作简单的小程序IP查看器功能

使用Python制作简单的小程序IP查看器功能

前言 说实话,查看电脑的IP,也挺无聊的,但是够简单,所以就从这里开始吧。IP地址在操作系统里就可以直接查看。但是除了IP地址,我们也想通过IP获取地理地址和网络运营商情况。IP地址和地...

python tkinter canvas使用实例

python tkinter canvas使用实例

这篇文章主要介绍了python tkinter canvas使用实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 注:在使用 cre...

你眼中的Python大牛 应该都有这份书单

你眼中的Python大牛 应该都有这份书单

在最新一期的话题中,80%读者认为Python是最好的编程语言,知乎上类似的问题也很多,例如如何入门Python?如何3个月内入门Python?虽然现在可以学习的Python途径...