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.

相关文章

用python3 返回鼠标位置的实现方法(带界面)

用python3 返回鼠标位置的实现方法(带界面)

点击获取后,返回2s后的鼠标位置,显示在文本框 (需要用pip命令安装所需的的库) (pip install 模块名 比如 安装pyautogui 模块 在cmd里面输入: pip in...

Python实现图片添加文字

在工作中有时候会给图上添加文字,常用的是PS工具,不过我想通过代码的方式来给图片添加文字。 需要使用的Python的图像库:PIL.更加详细的知识点如下: Imaga模块:用来创建,打开...

Django接收自定义http header过程详解

add by zhj: Django将所有http header(包括你自定义的http header)都放在了HttpRequest.META这个Python标准字典中,当然HttpR...

利用Python生成文件md5校验值函数的方法

前言 在linux有个命令叫做md5sum,能生成文件的md5值,一般情况下都会将结果记录到一个文件中用于校验使用,比如会这样使用: [crazyant@localhost Pyth...

Django 路由控制的实现

Django 路由控制的实现

一 Django中路由的作用 URL配置(URLconf)就像Django 所支撑网站的目录。它的本质是URL与要为该URL调用的视图函数之间的映射表;你就是以这种方式告诉Django,...