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中pass语句用法实例分析

本文实例讲述了python中pass语句用法。分享给大家供大家参考。具体分析如下: 1、空语句 do nothing 2、保证格式完整 3、保证语义完整 4、以if语句为例: C/C++...

使用python 对验证码图片进行降噪处理

使用python 对验证码图片进行降噪处理

首先贴一张验证码上来做案例: 第一步先通过二值化处理把干扰线去掉: from PIL import Image # 二值化处理 def two_value(): for i...

Python2.x版本中cmp()方法的使用教程

 cmp()方法返回两个数的差的符号: -1 如果 x < y, 0 如果 x == y, 或者 1 如果 x > y . 语法 以下是cmp()方法的语法:...

Django发送邮件和itsdangerous模块的配合使用解析

项目需求:用户注册页面注册之后,系统会发送一封邮件到用户邮箱,用户点击链接以激活账户,其中链接中的用户信息需要加密处理一下 其中激活自己邮箱的smtp服务的操作就不在加以说明,菜鸟教程...

Python入门学习之字符串与比较运算符

Python入门学习之字符串与比较运算符

Python字符串 字符串或串(String)是由数字、字母、下划线组成的一串字符。 一般记为 : s="a1a2···an"(n>=0) 它是编程语言中表示文本的数据类...