Python Glossary Русский

all Python 3Strings (str)function

str.find

search

Return the lowest index of the substring, or -1 if not found. Optional start/end.

Syntax

s.find(sub[, start[, end]])

Failure is signalled by -1, not None, so a truthiness test on the result is wrong twice over: a match at position 0 reads as false, while a miss at -1 reads as true. Always compare against -1 explicitly. If you only need to know whether the substring is there, the in operator is clearer; reach for find when you actually need the position.

Examples

print('hello'.find('l'))     # → 2
print('hello'.find('z'))     # → -1
print('hello'.find('l', 3))  # → 3

See also

Open in the full glossarydocs.python.orgReport an inaccuracy