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.

Syntax

The Syntax of find() method is:

str.find(sub, start, end)

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

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() method 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() method 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. Required fields are marked *

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