Find index of last occurrence of a substring in a string
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Finding the last position of a substring is a common parsing task when you need the rightmost separator, suffix marker, or repeated token. Most languages provide a built-in method for this, and using it is usually clearer and faster than writing a manual loop.
Use the Language Primitive First
In Python, the standard tool is str.rfind(). It searches from right to left and returns the starting index of the last match. If the substring is missing, it returns -1.
Output:
That index is useful because you can slice the string without extra parsing logic.
rfind() Versus rindex()
Python also offers rindex(), which behaves similarly but raises ValueError when the substring does not exist.
Choose between them based on control flow:
- use
rfind()when “not found” is an expected case - use
rindex()when the substring must exist and missing data should fail loudly
For most application code, rfind() is easier to work with because you can branch on -1.
Searching Within a Range
Both methods accept optional start and end positions. That matters when you need the last match in only part of the string.
This searches only within the first ten characters. Range-limited searches are useful when you parse structured filenames, log prefixes, or version identifiers that contain multiple separators.
A Real Parsing Example
A frequent use case is extracting a file extension. Using the last dot is safer than splitting on every dot because filenames can contain multiple dots.
Output:
That gives you the final extension rather than the first one.
Handling Case Sensitivity
String search is case-sensitive by default. If you want a case-insensitive last match, normalize both strings first.
That works well for user input, log processing, and simple report generation. If locale-specific rules matter, you may need more specialized handling, but lowercasing is fine for many technical strings.
When the Substring Has More Than One Character
The method returns the index where the matching substring starts, not where it ends.
Output:
This is especially important when you later slice the string. To move past the matched substring, add len(needle).
Alternatives in Other Languages
The concept is the same across ecosystems. JavaScript uses lastIndexOf, and Java uses lastIndexOf as well.
Those built-ins are still preferable to manually scanning characters unless you are implementing a custom parser for a specialized format.
A Simple Helper Function
If you perform the same pattern repeatedly, wrap it in a small helper.
This keeps calling code clean and centralizes the not-found behavior in one place.
Common Pitfalls
A common mistake is forgetting that rfind() returns -1 rather than throwing an exception. If you use that value directly in slicing without checking it, you may get incorrect output. Another mistake is assuming the result points to the end of the substring instead of its starting position. Developers also sometimes split the whole string into a list just to get the last separator, which is usually less direct than using rfind().
Summary
- Use
rfind()to get the last substring index without raising an exception. - Use
rindex()when missing data should raise an error. - Check for
-1before slicing. - Remember the returned index is the start of the match.
- Built-in right-to-left search methods are usually clearer than manual loops.

