str.find returns -1 when it does not find the substring.
>>> line = 'hi, this is ABC oh my god!!'
>>> line.find('?')
-1
While str.index raises ValueError:
>>> line.index('?')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
Both the functions behave the same way if a sub-string is found.
Answer from falsetru on Stack OverflowGeeksforGeeks
geeksforgeeks.org › python › difference-between-find-and-index-in-python
Difference Between find() and index() in Python - GeeksforGeeks
July 23, 2025 - Both methods are used to locate the position of a substring within a string but the major difference between find() and index() methods in Python is how they handle cases when the substring is not found.
Reddit
reddit.com › r/learnpython › why use index over find?
r/learnpython on Reddit: Why use index over find?
March 4, 2025 -
Won't index just make your code not work if it doesn't contain something while find still returns something so you can know if whatever you're looking for isn't in the thing?
Top answer 1 of 8
16
Why would you use either of them?
2 of 8
9
Raising an exception does not mean that the code will not work. Python has a try statement which lets you catch exceptions and do something with it. People may prefer to use index() as find() always returns an index even if the substring is not found (assuming you mean the str methods), so it's easy to write code that does not check and accidentally does the wrong thing. index() makes that impossible and requires you to catch the exception.
Can I use find() in question 28. Find the Index of the First Occurrence in a String? Apr 15, 2025
r/leetcode last yr.
Am I missing something with the find() function or perhaps another function? Oct 26, 2023
r/learnpython 2y ago
Why use index over find?
Why would you use either of them? More on reddit.com
Help with .index()? finding multiple instances of item
The easy way would be to just use enumerate on the list, and then manually iterate the list and find the indices yourself. More on reddit.com
00:50
Do YOU Know When to use FIND or INDEX - YouTube
07:38
Python String Index Method, (& find method, rindex & rfind) & Use ...
01:11
PYTHON : difference between find and index - YouTube
00:49
What is the Difference between Find() Method and Index() Method ...
01:59
#30 What is Difference between Find() and Index() Function in Python ...
06:22
Day 107 Difference between find and index in Python - YouTube
Top answer 1 of 3
146
str.find returns -1 when it does not find the substring.
>>> line = 'hi, this is ABC oh my god!!'
>>> line.find('?')
-1
While str.index raises ValueError:
>>> line.index('?')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
Both the functions behave the same way if a sub-string is found.
2 of 3
34
Also find is only available for strings where as index is available for lists, tuples and strings
>>> somelist
['Ok', "let's", 'try', 'this', 'out']
>>> type(somelist)
<class 'list'>
>>> somelist.index("try")
2
>>> somelist.find("try")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'find'
>>> sometuple
('Ok', "let's", 'try', 'this', 'out')
>>> type(sometuple)
<class 'tuple'>
>>> sometuple.index("try")
2
>>> sometuple.find("try")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'find'
>>> somelist2
"Ok let's try this"
>>> type(somelist2)
<class 'str'>
>>> somelist2.index("try")
9
>>> somelist2.find("try")
9
>>> somelist2.find("t")
5
>>> somelist2.index("t")
5
CodeSpeedy
codespeedy.com › home › difference between find() and index() in python
Difference between find() and index() in Python - CodeSpeedy
July 15, 2020 - Just like find(), index() method determines the position at which a substring is first found in a string. In the same way index() takes 3 parameters: substring that is to be searched, index of start and index of the end(The substring is searched between the start and end indexes of the string) out of which indexes of start and end are optional.
Educative
educative.io › answers › what-is-the-difference-between-string-find-and-index-method
What is the difference between String find() and index() method?
Python provides the following two built-in methods to find the first occurrence of a given substring in a string: ... Both methods have the same syntax. However, in the case where the required substring is not found in the target string, there is a significant difference in the output of both ...
CSEstack
csestack.org › home › difference between index() and find() in python
Difference between index() and find() in Python
July 9, 2020 - Note: The index() method can also be used to search the element in the Python list. ... We will use the same string and sub-string for find() method. samp_str = "CSEstack programmer" print(samp_str.find('stack')) ... You will find the same output result. Is there any difference between index() and find()?
TheLinuxCode
thelinuxcode.com › home › difference between find() and index() in python (practical patterns, pitfalls, and 2026-style error handling)
Difference Between find() and index() in Python (Practical Patterns, Pitfalls, and 2026-Style Error Handling) – TheLinuxCode
February 1, 2026 - If you remember only one thing, remember this: find() makes failure easy to ignore; index() makes failure hard to ignore. The most expensive bugs here aren’t about forgetting the difference between -1 and ValueError. The expensive bugs happen when someone checks the returned integer in a way that doesn’t match how integers behave in Python.
Iditect
iditect.com › programming › python-example › difference-between-find-and-index-in-python.html
Difference Between find( ) and index( ) in Python
s = "hello" result = s.index("l") ... not found in string.") # This block will be executed ... Use find() if you want a simple way to check for the presence of a substring in a string and don't want to handle exceptions....
TheLinuxCode
thelinuxcode.com › home › difference between find() and index() in python
Difference Between find() and index() in Python – TheLinuxCode
January 27, 2026 - This is not a replacement for find() or index(), but it’s a good option when you want a single split and a clear indicator of presence. Both methods are implemented in optimized C and run in linear time relative to the length of the string. In practice, the difference between them is negligible for typical inputs.
w3reference
w3reference.com › blog › python-why-do-the-find-and-index-methods-work-differently
Python: Why Do find() and index() Methods Behave Differently? Key Differences Explained — w3reference.com
Fails-Fast Behavior: index() raises an error immediately, which is useful when the substring must exist (e.g., parsing structured data like "YYYY-MM-DD" where "-" is expected). You need to check for the presence of a substring and handle its absence gracefully (without exceptions). Scenario: Searching for a keyword in user input. user_input = "I love Python programming" keyword = "Python" if user_input.find(keyword) != -1: print(f"Keyword '{keyword}' found!") else: print(f"Keyword '{keyword}' not found.")
Programiz
programiz.com › python-programming › methods › string › index
Python String index()
The only difference is that find() method returns -1 if the substring is not found, whereas index() throws an exception. ... Substring 'is fun': 19 Traceback (most recent call last): File "<string>", line 6, in result = sentence.index('Java') ValueError: substring not found · Note: Index in ...
All In The Diffrence
allinthedifference.com › difference-between-find-and-index-in-python
Understanding the Key Differences Between Find() and Index() in Python: A Comprehensive Guide
June 25, 2024 - It’s clear that choosing between these two depends largely on your specific needs: if you’re working with larger strings or repetitive tasks where misses might occur, find()’s non-interruptive nature is a godsend; but when certainty of matches is high and speed matters, nothing beats index(). Now armed with this knowledge about their efficiency differences, you’re well-prepared for coding more effective Python projects!