首页  >  文章  >  后端开发  >  分享一篇Python中字符串函数 (partition)详解

分享一篇Python中字符串函数 (partition)详解

零下一度
零下一度原创
2017-05-22 17:11:024372浏览

这里提到partition()函数,它是干什么的呢,其实这个函数跟split差不多,都是搞切割的。

partition(...)
    S.partition(sep) -> (head, sep, tail)
    
    Search for the separator sep in S, and return the part before it,
    the separator itself, and the part after it.  If the separator is not
    found, return S and two empty strings.

举个例子:

>>> a = 'changzhi1990'
>>> a.rpartition('h')
('changz', 'h', 'i1990')


可以看到返回了一个三元的tuple,分别是‘h’ 的左边的字符串,分割符‘h’本身,和分割符‘h’的右边的字符串。注意:r 代表从右向左开始匹配。

>>> a = 'changzhi1990'
>>> a.partition('h')
('c', 'h', 'angzhi1990')

这里是从左到右开始匹配的。

【相关推荐】

1. Python中partition字符串函数实例教程

2. 分享一个字符串函数(partition)的实例代码

3. MySQL之-数据表分区技术PARTITION的代码示例浅析

以上是分享一篇Python中字符串函数 (partition)详解的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn