partition python_Python的partition字符串函数 | 学步园

在2.5版中新增加了一个叫rpartition的函数,看一下说明文档:

rpartition(...)

S.rpartition(sep) -> (head, sep, tail)

Search for the separator sep in S, starting at the end of S, and return

the part before it, the separator itself, and the part after it. If the

separator is not found, return two empty strings and S.

(END)

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')

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值