Python rindex() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

rindex() searches from the right and returns the index of the last place where the substring starts, counted from the left. It works exactly like rfind(), with one difference: when the substring is missing it raises ValueError instead of returning -1.

That makes it the strict choice. Use it when a missing substring would be a bug and you want the program to stop loudly, and use rfind() when a missing substring is a normal case. Optional start and end limit the search to a slice.

text.rindex() is a method of the built-in str type: call it on a string with dot notation. No import is needed, and it never changes the original string.

Syntax

Pass the substring to look for. start and end are optional.

python
pos = text.rindex(sub)
pos = text.rindex(sub, start, end)
CallWhat it does
"hello".rindex("l")Returns 3, the index of the last l.
"a/b/c".rindex("/")Returns 3, the position of the last slash.
"hello".rindex("z")Raises ValueError. The substring is not in the string.

Use rfind() to get -1 instead of an error, and index() for the first match.

Library declarations
python
str.rindex(sub[, start[, end]]) -> int
# returns: the highest index where sub starts
# raises: ValueError if sub is not found; TypeError if sub is not a string

Parameters

ParameterDescription
subRequired. The substring to look for. It must be a string.
startOptional. Index where the search area begins. Default 0.
endOptional. Index where the search area stops, exclusive. Default is the end of the string.

Return Value

Return ValueDescription
intThe index of the last match, counted from the left. If there is no match, ValueError is raised instead.

Python Examples

Example 1 — Find the Last Match or Fail

Find the last letter, then handle a substring that is missing.

python
word = "mississippi"

print(word.rindex("s"))
print(word.rindex("ss"))

try:
    word.rindex("z")
except ValueError:
    print("z is not in the word")

How It Works

  1. The last s. In "mississippi", the final s is at index 6.
  2. A longer substring. The last "ss" starts at index 5, which is one earlier than the last single s.
  3. A missing substring raises. "z" is not present, so ValueError is raised and the except block prints the message.

Example 2 — Split a Setting at the Last Equals Sign

Cut a line at its last = so that earlier equals signs stay in the key.

python
line = "name=Ada=Lovelace"

cut = line.rindex("=")
key = line[:cut]
value = line[cut + 1:]

print(cut)
print(key)
print(value)

How It Works

  1. The last equals sign. rindex("=") returns 8, skipping the earlier one at index 4.
  2. Key is the left part. line[:cut] keeps everything before the cut, including the first =.
  3. Value is the right part. line[cut + 1:] starts just after the equals sign.

Example 3 — Choose Between the Two

Wrap rindex() in a helper that returns None, and compare it with rfind().

python
def last_slash(path):
    try:
        return path.rindex("/")
    except ValueError:
        return None


print(last_slash("a/b/c"), last_slash("abc"))
print("abc".rfind("/"))

How It Works

  1. A safe helper. The function returns the index when there is a slash and None when rindex() raises.
  2. Two results. "a/b/c" gives 3, and "abc" has no slash, so the helper returns None.
  3. rfind() needs no helper. It reports a missing slash as -1 without raising anything.

Frequently Asked Questions

It returns the index of the last occurrence of a substring. If the substring is not found, it raises ValueError.
They return the same index when the substring exists. When it is missing, rindex() raises ValueError and rfind() returns -1.
index() returns the first match, while rindex() returns the last match. Both raise ValueError if the substring is missing.
Check with sub in text first, catch ValueError with try/except, or use rfind(), which returns -1 instead.
Yes. Pass start and end, for example text.rindex("a", 2, 8), and only that slice of the string is searched.

Next: rjust()

Return the string right-aligned in a field of a given width, padded on the left.

rjust() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful