partition()、rpartition()

partition() 根据指定的分隔符 (sep) 将字符串进行分割,从字符串左边开始索引分隔符 sep, 索引到则停止索引,返回的是一个包含三个元素的元组 (tuple),即 (head, sep, tail)。
repartiton()是从后往前开始分割

点击查看代码
s = 'hello, welcome to the world'
#遇到第一个分隔符后就停止索引
s.partition('e')
('h', 'e', 'llo, welcome to the world')

#没有遇到分隔符,返回原字符串和两个空字符串
s.partition('f')
('hello, welcome to the world', '', '')

s.rpartition('e')
('hello, welcome to th', 'e', ' world')

s.rpartition('f')
('', '', 'hello, welcome to the world')
posted @ 2022-07-02 17:50  灵、主  阅读(61)  评论(0)    收藏  举报