Python String find()

The Python String find() method is a built-in function that returns the index of the first occurrence of a substring in a given string. If not found, it returns -1.

In this article, we will learn about the Python String find() method with the help of examples.

find() Syntax

The Syntax of find() method is:

str.find(sub, start, end)

find() Parameters

The find() method can take three parameters.

  • sub – substring that needs to be searched in the given string.
  • start (optional) – starting position where the substring needs to be searched within the string
  • end (optional) – ending position where the substring needs to be searched within the string
Note: If the start and end indexes are not provided, by default, it takes 0 as the starting index and str.length-1 as the end index

find() Return Value

The find() method returns an integer value, an index of a substring.

  • If the substring is found in the given string, the find() method will return the index of the first occurrence of the substring.
  • If the substring is not found in the given string, it returns -1
Python Find() Vs Rfind()
Python String find() 2

Difference between find() method and index() method

The find() method is similar to index() method. The only major difference is that the find() method returns -1 if the substring is not found in a given string, whereas the index() method will raise the ValueError: substring not found exception.

Example 1: find() without any arguments

text = 'she sells seashells on the seashore'

# find the index of first occurence of substring 'sea'
output = text.find('sea')
print("The index of first occurrence of 'sea' is:", output)

# find the index of character 'lls'
output = text.find('lls')
print("The index of first occurrence of 'lls' is:", output)

# find the index of substring 'fish', returns -1
output = text.find('fish')
print("The index of first occurrence of 'fish' is:", output)

Output

The index of first occurrence of 'sea' is: 10
The index of first occurrence of 'lls' is: 6
The index of first occurrence of 'fish' is: -1

Example 2: find() with start and end Arguments

text = 'she sells seashells on the seashore'

# find the index of first occurence of substring 'sea'
output = text.find('sea', 25)
print("The index of first occurrence of 'sea' is:", output)

# find the index of character 'lls'
output = text.find('lls', 1, 10)
print("The index of first occurrence of 'lls' is:", output)

# find the index of substring 'fish', returns -1
output = text.find('fish', 1, 50)
print("The index of first occurrence of 'fish' is:", output)

# find the index of substring 's'
output = text.find('s', 1, -20)
print("The index of first occurrence of 's' is:", output)

Output

The index of first occurrence of 'sea' is: 27
The index of first occurrence of 'lls' is: 6
The index of first occurrence of 'fish' is: -1
The index of first occurrence of 's' is: 4
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Numpy.argmax() In Python

numpy.argmax() in Python

Table of Contents Hide SyntaxParametersReturn ValueFinding the maximum element from a matrix with Python numpy.argmax()Using np.unravel_index on argmax outputFinding Maximum Elements along columns using Python numpy.argmax() The numpy.argmax() function returns the indices…
View Post
Python String Isalnum()

Python String isalnum()

Table of Contents Hide What is Alphanumeric?isalnum() Syntaxisalnum() Parameters isalnum() Return valueExample 1: Python string isalnum() working examplesExample 2: Program to check if the string is alphanumeric Python string isalnum() method…
View Post