关于python:python中rindex函数是什么

6次阅读

共计 296 个字符,预计需要花费 1 分钟才能阅读完成。

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 函数的介绍,心愿对大家有所帮忙。

正文完
 0