Python String find()

The find() method returns the index of first occurrence of the substring (if found). If not found, it returns -1.

Example


find() Syntax

The syntax of the find() method is:

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

find() Parameters

The find() method takes maximum of three parameters:

  • sub - It is the substring to be searched in the str string.
  • start and end (optional) - The range str[start:end] within which substring is searched.

find() Return Value

The find() method returns an integer value:

  • If the substring exists inside the string, it returns the index of the first occurence of the substring.
  • If a substring doesn't exist inside the string, it returns -1.

Working of find() method

How find() and rfind() works in Python?
Working of Python string's find() and rfind() methods

Example 1: find() With No start and end Argument

Output

Substring 'let it': 11
Substring 'small ': -1
Contains substring 'be,'

Example 2: find() With start and end Arguments

Output

-1
3
-1
9