Python rfind and rindex

str.rfind(sub[, start[, end]])

Return the highest index in the string where substring sub is found, such that sub is contained within s[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 on failure.

str.rindex(sub[, start[, end]])

Like rfind() but raises ValueError when the substring sub is not found.


类似lastIndexOf,返回子串在指定父串最后出现的位置,区别是当子串不存在时,rindex返回错误,而rfind返回-1

示例

s_str = "/img/qrcode_for_gh.jpg"
print(s_str.rindex("/"))
print(s_str.rfind("/"))
print(s_str.rfind("\\"))
print(s_str.rindex("\\"))

4
4
-1
Traceback (most recent call last):
File “D:/work/PyCharm/cn/com/icbc/request/html_to_text.py”, line 69, in
print(s_str.rindex(“\”))
ValueError: substring not found

https://docs.python.org/3/library/stdtypes.html?highlight=rfind#str.rfind

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值