python教程

python中rindex函数是什么

python教程 51源码 2022-11-29 人阅读

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

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

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

版权声明:文章搜集于网络,如有侵权请联系本站,转载请说明出处:https://www.51yma.cn/jiaocheng/python/810.html
文章来源:Python学习网-https://www.py.cn//jishu/jichu/34264.html
标签 python rindex