当前位置: 首页 > 图灵资讯 > 行业资讯> python中rindex函数是什么

python中rindex函数是什么

来源:图灵python
时间: 2024-06-07 14:11:53

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

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

实例

mystr='helloworldandhellopython'

#1.查找整个字符串
print(mystr.rindex('python'))

#输出结果
22

#2.在字符串的指定位置搜索
print(mystr.rindex('python',0,20))

#返回结果
报异常:ValueError:substringnotfound

以上是python中rindex函数的介绍,希望对大家有所帮助。更多Python学习指导:python基础教程

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