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

Get notified of the best deals on our WordPress themes.

You May Also Like
Xor In Python

XOR in Python

XOR Operator in Python is also known as “exclusive or”  that compares two binary numbers bitwise if two bits are identical XOR outputs as 0 and when two bits are different then…
View Post
Python List Index()

Python List index()

Table of Contents Hide Syntax of List index() index() ParametersReturn Value from List index()Example 1: Find the index of the elementExample 2: Index of the Element not Present in the List…
View Post
Nxnxn Matrix Python

nxnxn matrix python

Table of Contents Hide What is NxNxN?How to Create NxNxN Matrix in Python?Create NxN Matrix in Python 3 with Non Duplicating numbersCreate NxNxN matrix in Python using numpy In this…
View Post
Python String Index()

Python String index()

Table of Contents Hide index() Syntaxindex() Parametersindex() Return ValueDifference between index() method and find() methodExample 1: Find the index of a string in PythonExample 2: If string is not found…
View Post
How To Rename A File In Python

How to Rename a file in Python?

Table of Contents Hide Python Rename FileExample to rename a file in PythonPython Rename Multiple FilesExample to Rename Multiple Files in Python The os module in Python comes in handy…
View Post