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