Python rindex() method
Description
Python rindex() returns the last position at which the substring str occurs in a string. If no matching string is found, an exception is raised. You can specify the optional parameters [beg:end] to set the search range.
Syntax
Syntax of the rindex() method:
str.rindex(str, beg=0 end=len(string))
Parameters
- str -- the string to find
- beg -- the position to start searching, defaults to 0
- end -- the position to end searching, defaults to the length of the string.
Return Value
Returns the position of the last occurrence of the substring str in a string, and raises an exception if no match is found.
Example
The following example shows how to use the rindex() function:
#!/usr/bin/python
str1 = "this is string example....wow!!!";
str2 = "is";
print str1.rindex(str2);
print str1.index(str2);
The output of the above example is:
5
2
Python Strings