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内置模块collections

collections是Python内建的一个集合模块,提供了许多有用的集合类。 1、namedtuple python提供了很多非常好用的基本类型,比如不可变类型tuple,我们可以轻...

总结Python编程中三条常用的技巧

在 python 代码中可以看到一些常见的 trick,在这里做一个简单的小结。 json 字符串格式化 在开发 web 应用的时候经常会用到 json 字符串,但是一段比较长的 jso...

Python闭包函数定义与用法分析

本文实例分析了Python闭包函数定义与用法。分享给大家供大家参考,具体如下: python的闭包 首先python闭包的作用,一个是自带作用域,另一个是延迟计算。 闭包是装饰器的基础。...

python实现微信发送邮件关闭电脑功能

Python 通过微信邮件实现电脑关机,供大家参考,具体内容如下 通过手机微信发送QQ邮件给sina邮箱,然后利用python的pop3定时检查sina邮箱的邮件主题以及邮件来源,并在电...

Java与Python两大幸存者谁更胜一筹呢

Java与Python两大幸存者谁更胜一筹呢

在学习编程语言上,相信很多人都纠结过学哪种语言更好?其实,在选择是更多的时候我们更多是需要看自己更适合。本篇文章,千锋武汉小编与大家共同讨论的编程语言,或许更多的是限于python和Ja...