Last Updated : 26 Apr, 2025
find() method in Python returns the index of the first occurrence of a substring within a given string. If the substring is not found, it returns -1. This method is case-sensitive, which means "abc" is treated differently from "ABC". Example:
Python
s = "Welcome to GeekforGeeks!"
index = s.find("GeekforGeeks")
print(index)
Explanation: The substring "GeeksforGeeks" starts at index 11 in the string "Welcome to GeeksforGeeks!". Hence, find() method returns 11
Syntax of find() methods.find(substring, start, end))
Parameter:
Return Value:
Example 1: We can limit the search to a specific portion of the string by providing start and end parameters.
Python
s = "abc abc abc"
index = s.find("abc", 4)
print(index)
Explanation:
Example 2: The find() method is case-sensitive, so uppercase and lowercase letters are treated differently.
Python
s = "Python is fun"
index = s.find("python")
print(index)
Explanation: Since "python" (lowercase) does not match "Python" (uppercase), the method returns -1.
Example 3: In this example, we are searching for the first occurrence of a substring "abc" in a string that contains multiple spaces between the words.
Python
s = "abc abc abc"
res = s.find("abc")
print(res)
Explanation: The substring "abc" starts at index 0 in the string "abc abc abc", so find() returns 0.
find() vs index()Both find() and index() methods locate a substring within a string. However, they differ in behavior when the substring is not found.
To learn more, please refer to "Difference Between find( ) and index( ) in Python".
Related Articles:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4