Python 3 reference · String Methods

Python String find() Method

Return the lowest index where a substring is found, or -1 if it isn't. Optional start and end limit the search.

Python 3 Since 3.0

Syntax

str.find(sub, start=0, end=len(s))

Parameters

ParameterDescription
sub The substring to look for.
start, end optional Only search this slice.

Return value

The index (an int), or -1.

Examples

Find a substringTry it yourself
text = "hello world"
print(text.find("o"), text.find("o", 5), text.find("z"))

Output

4 7 -1
Split around a characterTry it yourself
email = "[email protected]"
at = email.find("@")
print(email[:at], email[at + 1:])

Output

ada example.com
Just checking? Use inTry it yourself
print("cat" in "concatenate")  # use in for yes/no checks

Output

True

find() vs index()

find() returns -1 when nothing is found; index() raises ValueError. Be careful: -1 is also a valid index (the last character), so always compare with == -1 before slicing.

Version history

VersionChange
Python 3.0Added