str.rfind() Reverse find

Last updated: 2026-09-22

str.rfind() Reverse find

str.rfind(sub) returns the highest index where sub is found; -1 if not found.

CategoryString Methods
KindBuilt-in Type Method
Python Versionall

📝 Syntax

str.rfind(sub[, start[, end]])

⚙️ Parameters

sub RequiredThe substring to search for.
start OptionalStart index of the range (inclusive); None means the beginning.(Default:None)
end OptionalEnd index of the range (exclusive); None means the end.(Default:None)

Returns:int. The highest index where sub is found, or -1 if not found.

Mutates the original:No (returns a new object)

▶ Example

Edit the code, press Run, and see the result in the output panel:

Output:

12
-1
More case studies coming soon — check back later.

❓ FAQ

QWhat is the difference between rfind() and find()?
Arfind() searches from right to left and returns the position of the last occurrence; when not found it also returns -1 without raising an exception.
QHow do I find the last separator in a path?
AUse rfind(): '/a/b/c.py'.rfind('/') returns the index of the last '/', often used to split at the last occurrence.
QDoes it raise an exception when not found?
ANo, it returns -1. If you want an error when not found, use rindex().
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏