str.find()
str.find(sub[, start[, end]]) Returns:
int · Updated March 13, 2026 · String Methods stdlib string search
The .find() method searches for a substring within a string and returns the lowest index where the substring is found. Unlike .index(), it returns -1 when the substring does not exist instead of raising a ValueError.
Syntax
str.find(sub[, start[, end]])
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
| sub | str | — | The substring to search for |
| start | int | 0 | Beginning index to search from |
| end | int | len(str) | Ending index to search up to |
Return Value
Returns an integer representing the lowest index where the substring is found. Returns -1 if the substring is not present in the string.
Examples
Basic search
text = "Hello, world"
print(text.find("world"))
# 7
print(text.find("Python"))
# -1
Using start parameter
text = "Hello, hello, hello"
print(text.find("hello"))
# 0
print(text.find("hello", 1))
# 7
Using start and end
text = "Hello, world"
print(text.find("l", 0, 5))
# 2
print(text.find("l", 0, 3))
# 2
Practical use case
def highlight_word(text, word):
idx = text.find(word)
if idx != -1:
return text[:idx] + "[" + word + "]" + text[idx + len(word):]
return text
print(highlight_word("Find the word in this sentence", "word"))
# Find the [word] in this sentence
Common Patterns
Check before using
text = "Python is great"
if text.find("is") != -1:
print("Found!")
Find all occurrences
text = "apple banana apple apple"
start = 0
while True:
idx = text.find("apple", start)
if idx == -1:
break
print(f"Found at {idx}")
start = idx + 1