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 执行文件时额外参数获取的实例

如下所示: def usage(): print(' * usage:') print(' * -c [val] : exporter_conf filepath, def...

python+opencv实现动态物体追踪

python+opencv实现动态物体追踪

简单几行就可以实现对动态物体的追踪,足见opencv在图像处理上的强大。 python代码: import cv2 import numpy as np camera=cv2.V...

python中文件变化监控示例(watchdog)

在python中文件监控主要有两个库,一个是pyinotify ( https://github.com/seb-m/pyinotify/wiki ),一个是watchdog(http:...

Django框架实现的普通登录案例【使用POST方法】

Django框架实现的普通登录案例【使用POST方法】

本文实例讲述了Django框架实现的普通登录。分享给大家供大家参考,具体如下: 1.显示登录页面 a.设计url,通过浏览器访问http://127.0.0.1:8000//login的...

Python玩转Excel的读写改实例

摘要: 利用xlrd读取excel 利用xlwt写excel 利用xlutils修改excel 利用xlrd读取excel 先需要在命令行中pip install xlr...