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调用支付宝支付接口流程

python调用支付宝支付接口流程

项目演示: 一、输入金额 二、跳转到支付宝付款 三、支付成功 四、跳转回自己网站 在使用支付宝接口的前期准备: 1、支付宝公钥 2、应用公钥 3、应用私钥 4、APPID 5、D...

python3实现逐字输出的方法

如下所示: import sys,time def print_one_by_one(text):     sys.stdout.write("\r "...

10 行 Python 代码教你自动发送短信(不想回复工作邮件妙招)

10 行 Python 代码教你自动发送短信(不想回复工作邮件妙招)

最近工作上有个需求,当爬虫程序遇到异常的时候,需要通知相应的人员进行修复。如果是国外可能是通过邮件的方式来通知,但国内除了万年不变的 qq 邮箱,大部分人都不会去再申请其他的账号,qq...

Python实现把utf-8格式的文件转换成gbk格式的文件

需求:将utf-8格式的文件转换成gbk格式的文件 实现代码如下: 复制代码 代码如下: def ReadFile(filePath,encoding="utf-8"):  &...

python3 实现验证码图片切割的方法

python3 实现验证码图片切割的方法

切割前图片 切割后四个图片 代码 #coding:utf8 import os from PIL import Image,ImageDraw,ImageFile import...