首页 > 文章列表 > python中rindex函数是什么

python中rindex函数是什么

Python rindex
160 2022-08-07

1、rindex函数主要用于在给定的字符串中找到子字符串是否存在。如果找到,返回子串的第一个索引位置,否则会直接抛出异常。

2、rindex开始从字符串的右侧搜索,但返回的索引仍然从左侧计算。

实例

mystr = 'hello world and hello python'
 
#1.在整个字符串中查找
print(mystr.rindex('python'))
 
#输出结果
22
 
#2.在字符串的指定位置内查找
print(mystr.rindex('python',0,20))
 
#返回结果
报异常:ValueError:substring not found

(推荐操作系统:windows7系统、Python 3.9.1,DELL G3电脑。)