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.

相关文章

Django的HttpRequest和HttpResponse对象详解

本文研究的主要是Django的HttpRequest和HttpResponse对象的相关内容,具体如下。 请求一张页面时,Django把请求的metadata数据包装成一个HttpReq...

Python深拷贝与浅拷贝用法实例分析

本文实例讲述了Python深拷贝与浅拷贝用法。分享给大家供大家参考,具体如下: 1、对象的赋值 对象的赋值实际上是对象之间的引用:当创建一个对象,然后将这个对象赋值给另外一个变量的时候,...

Python定时任务工具之APScheduler使用方式

APScheduler (advanceded python scheduler)是一款Python开发的定时任务工具。 文档地址 apscheduler.readthedoc...

python pygame实现五子棋小游戏

今天学习了如何使用pygame来制作小游戏,下面是五子棋的代码,我的理解都写在注释里了 import pygame # 导入pygame模块 print(pygame.ver) #...

Python常用库大全及简要说明

环境管理 管理 Python 版本和环境的工具 p:非常简单的交互式 python 版本管理工具。官网 pyenv:简单的 Python 版本管理工具。官网 Vex:可以在虚拟环境中...