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 Paramiko模块的使用实际案例

本文研究的主要是Python Paramiko模块的使用的实例,具体如下。 Windows下有很多非常好的SSH客户端,比如Putty。在python的世界里,你可以使用原始套接字和一些...

python-opencv颜色提取分割方法

python-opencv颜色提取分割方法

1.用于简单的对象检测、跟踪 2.简单前背景分割 #encoding:utf-8 #黄色检测 import numpy as np import argparse import cv...

python去重,一个由dict组成的list的去重示例

背景:有一个list,里面的每一个元素都是dict,根据某一个key进行去重,在这里,key代表question #!/usr/bin/env python # -*- coding...

详解Python的单元测试

如果你听说过“测试驱动开发”(TDD:Test-Driven Development),单元测试就不陌生。 单元测试是用来对一个模块、一个函数或者一个类来进行正确性检验的测试工作。 比如...

Python实现提取XML内容并保存到Excel中的方法

Python实现提取XML内容并保存到Excel中的方法

本文实例讲述了Python实现提取XML内容并保存到Excel中的方法。分享给大家供大家参考,具体如下: 最近做一个项目是解析XML文件,提取其中的chatid和lt、timestamp...