Easy Tutorial
❮ Python Func Number Shuffle Python Cube Sum ❯

Python3 rindex() Method

Python3 Strings


Description

The rindex() method returns the last occurrence of the substring str in the string. If no matching substring is found, it raises an exception. You can specify optional parameters [beg:end] to set the search interval.

Syntax

Syntax for the rindex() method:

str.rindex(str, beg=0, end=len(string))

Parameters

Return Value

Returns the last occurrence of the substring str in the string. If no matching substring is found, it raises an exception.

Example

The following example demonstrates the use of the rindex() function:

#!/usr/bin/python3
str1 = "this is really a string example....wow!!!"
str2 = "is"

print (str1.rindex(str2))
print (str1.rindex(str2,10))

The output of the above example is:

5
Traceback (most recent call last):
  File "test.py", line 6, in <module>
    print (str1.rindex(str2,10))
ValueError: substring not found

Python3 Strings

❮ Python Func Number Shuffle Python Cube Sum ❯