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编程开发之textwrap文本样式处理技巧

本文实例讲述了python编程开发之textwrap文本样式处理技巧。分享给大家供大家参考,具体如下: 在看python的API的时候,发现python的textwrap在处理字符串样式...

flask框架中勾子函数的使用详解

在客户端和服务器交互的过程中,有些准备工作或扫尾工作需要处理,比如: 在请求开始时,建立数据库连接; 在请求开始时,根据需求进行权限校验; 在请求结束时,指定数据的交互格式...

Python简单实现子网掩码转换的方法

本文实例讲述了Python简单实现子网掩码转换的方法。分享给大家供大家参考,具体如下: 这里实现将子网掩码长度转换为具体的子网掩码地址: def exchange_maskint(m...

Python实现PS滤镜特效之扇形变换效果示例

Python实现PS滤镜特效之扇形变换效果示例

本文实例讲述了Python实现PS滤镜特效之扇形变换效果。分享给大家供大家参考,具体如下: 这里用 Python 实现 PS 滤镜中的一种几何变换特效,称为扇形变换,将图像扭曲成一个扇形...

详解Python中表达式i += x与i = i + x是否等价

详解Python中表达式i += x与i = i + x是否等价

前言 最近看到一个题目,看似很简单,其实里面有很深的意义,题目是Python 表达式 i += x 与 i = i + x 等价吗?如果你的回答是yes,那么恭喜你正确了50%,为什么说...