Python Function Reference

str.find

Find the first occurrence of the substring sub in the string.

Python 3.13
str.find(sub[, start[, end]])

Parameters

substr
The substring to find.
startintoptional
The index where the search starts.
endintoptional
The index before which the search stops.

Return value

The index of the first occurrence, or -1 if the substring is not found.

Examples

Position of the first match

Python 3.13
print('hello'.find('l'))
2

Substring not found

Python 3.13
print('hello'.find('z'))
-1

Details

To only check whether a substring is present, use the in operator: "ll" in "hello". If a missing substring is an error, str.index is more convenient.

See also

Where to learn