Skip to content
String Methods

find()

Find the first position of some text without raising an error when it is missing.

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

str.find() returns the lowest index where substring sub is found within the slice [start, end].

If sub is not found, it returns -1 instead of raising an exception.

Parameters

NameDescription
subThe text to look for.
startOptional starting position.
endOptional position where the search stops.

Returns

The first index, or -1 if the text is not found.

Try it

Run it and change it
text = "banana"
print(text.find("na"))

Worth knowing

  • find() vs index(): Use find() when a missing substring should return -1 for boolean checks; use index() if missing values should raise a ValueError.

Related entries