Python String rindex()方法返回字符串中子字符串的最高索引(如果找到该子字符串)。否则,它会引发一个例外。
null
语法:
str.rindex(sub,start,end)
参数:
- sub:它是需要在给定字符串中搜索的子字符串。
- 开始:需要在字符串中检查sub的开始位置。
- 结束:字符串中需要检查后缀的结束位置。
注: 如果没有提供开始和结束索引,那么默认情况下,它将0和length-1作为开始和结束索引,其中结束索引不包括在搜索中。
返回:
如果找到子字符串,则返回字符串中子字符串的最高索引。否则就会引发一个例外。
错误和例外:
ValueError:当在目标字符串中找不到参数字符串时,会引发此错误。
例1
input: text = 'geeks for geeks' result = text.rindex('geeks')output: 10input: text = 'geeks for geeks' result = text.rindex('ge')output: 10
Python3
# Python code to demonstrate working of rindex() text = 'geeks for geeks' result = text.rindex( 'geeks' ) print ( "Substring 'geeks':" , result) |
输出:
Substring 'geeks': 10
例2
Python3
# Python code to demonstrate error by rindex() text = 'geeks for geeks' result = text.rindex( 'pawan' ) print ( "Substring 'pawan':" , result) |
错误:
Traceback (most recent call last): File "/home/dadc555d90806cae90a29998ea5d6266.py", line 6, in result = text.rindex('pawan')ValueError: substring not found
例3
Python3
# Python code to demonstrate working of rindex() # with range provided quote = 'geeks for geeks' # Substring is searched in ' geeks for geeks' print (quote.rindex( 'ge' , 2 )) # Substring is searched in 0 to 10 range print (quote.rindex( 'geeks' , 0 , 10 )) |
输出:
100
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END