Python String find() Method
The Python string find() method searches for a substring inside a string.
It returns the index of the first match. If the text is not found, it returns -1.
Use find() when you want the position of some text and it is normal for that text to be missing.
text = "hello world"
position = text.find("world")
print(position) # 6
missing = text.find("python")
print(missing) # -1
Use
find()when you want the position of a substring. It returns-1if the substring is not found.
What find() does
find() is a string method that:
- Searches a string for a substring
- Returns the index of the first match
- Returns
-1if no match is found - Does not change the original string
Example:
text = "banana"
print(text.find("na")) # 2
print(text.find("x")) # -1
print(text) # banana
Here, "na" first appears at index 2.
Basic syntax
The syntax is:
string.find(value, start, end)
Parameters
value: the text to search forstart: optional starting positionend: optional ending position
start and end let you search only part of the string.
Basic example:
text = "hello world"
print(text.find("o")) # 4
print(text.find("o", 5)) # 7
print(text.find("o", 5, 7)) # -1
What the return value means
The return value from find() is always an integer.
- A number
0or greater means the substring was found - The number is the starting position of the match
-1means the substring was not found
Example:
text = "python"
print(text.find("py")) # 0
print(text.find("th")) # 2
print(text.find("z")) # -1
A common beginner mistake is expecting find() to return True or False.
It does not return a Boolean value. It returns the position of the match.
Using start and end
Use start to skip the earlier part of a string.
Use end to stop searching before a certain position.
The end position is not included in the search.
text = "one two three two"
print(text.find("two")) # 4
print(text.find("two", 5)) # 14
print(text.find("two", 0, 7)) # 4
print(text.find("two", 0, 4)) # -1
This is useful when you only want to search inside part of a string before slicing or processing it further.
If you want to split text after finding a separator, see how to split a string in Python or the split() string method.
Case sensitivity
find() is case-sensitive.
That means "Hello" and "hello" are treated as different text.
text = "Hello World"
print(text.find("Hello")) # 0
print(text.find("hello")) # -1
If you want a case-insensitive search, convert both values to the same case first.
text = "Hello World"
print(text.lower().find("hello")) # 0
find() vs index()
find() and index() are similar, but they behave differently when the substring is missing.
find()returns-1index()raises aValueError
Use find() when missing text is normal.
Use index() when the value must exist.
text = "hello"
print(text.find("x")) # -1
text = "hello"
print(text.index("x")) # ValueError
If you want to learn the difference in more detail, see the index() string method. If your code crashes because of index(), read how to fix ValueError: substring not found.
Common beginner use cases
Beginners often use find() for simple string search tasks like these:
- Check whether part of a string exists
- Get the position of a word
- Find separators like commas, colons, or spaces
- Search before slicing a string
Example: finding a colon before slicing
text = "name:Alice"
colon_pos = text.find(":")
if colon_pos != -1:
label = text[:colon_pos]
value = text[colon_pos + 1:]
print(label) # name
print(value) # Alice
If you only want to check whether text exists, you may also want to read how to check if a string contains a substring in Python.
Common mistakes
These are some common beginner mistakes when using find():
- Expecting
find()to returnTrueorFalseinstead of an index - Forgetting that the method returns
-1when the substring is missing - Using
find()without handling case differences - Confusing
find()withindex() - Assuming
find()changes the string
Example of a mistake:
text = "hello"
if text.find("h"):
print("Found")
else:
print("Not found")
This prints:
Not found
That happens because "h" is found at index 0, and 0 is treated as false in an if statement.
A safer version is:
text = "hello"
if text.find("h") != -1:
print("Found")
else:
print("Not found")
You can also debug string search problems with simple checks like these:
print(text.find('word'))
print(repr(text))
print(text.lower().find('word'))
print(len(text))
print(text.find('word', start, end))
These help you check:
- The actual result from
find() - Hidden spaces or newline characters with
repr() - Case differences with
lower() - The string length
- Whether your
startandendvalues are correct
FAQ
What does Python string find() return if the text is not found?
It returns -1.
Does find() return the first match or all matches?
It returns the index of the first match only.
Is Python find() case-sensitive?
Yes. Uppercase and lowercase letters are treated as different.
What is the difference between find() and index() in Python?
find() returns -1 if not found, while index() raises a ValueError.
Can I use find() to search only part of a string?
Yes. Use the optional start and end arguments.