Signup/Sign In
LAST UPDATED: JUNE 4, 2020

Python String find()

If anyone wants to find a substring in a string or a letter in a string then for that purpose Python String find() is used.

  • Python String find() is an inbuilt function.

  • If you are finding a substring that exists inside a string then this function returns the index of the first occurrence of the string; if the substring does not exist in a string then the function returns -1.

  • Thus String find() helps in finding the index of the substring inside a string.

  • If the starting and ending index of a string is not given in that case default value of start is 0 and length of the string is considered as -1 and also in that case end index is not included in the search

  • String find() always returns an integer value.

  • With this method, we can find both substring of a string as well as a letter of a string.

Python String find(): Syntax

Below we have a basic syntax of String find() in Python:

string.find(value, start, end)

Python String find(): Parameters

Given below is a description of Parameters of find() as there are three parameters:

  • value

It is a mandatory parameter; it is used to define the substring or character we are searching for in a string.

  • start

It is used to tell us from which index the search begins and it is an optional parameter its default value is -1.

  • end

It is used to tell us at which index the search ends and this parameter is also an optional parameter.

Python String find(): Returned Values

This Python String find() always returns an integer value.

  • If the substring or a letter we are looking in a string exists in that string then, in that case, this method returns the index of the first occurrence of a substring or a letter.

  • If the substring or a letter we are looking in a string does not exist in that string then, in that case, this method returns -1.

Python String find(): Basic Example to find substring in a string

Below we have an example to show the working of String find() function:

str = 'StudyTonight is a platform to learn coding online'
index = str.find('Tonight')
print(index)

In the above example, the 'start' parameter is not defined in that case the default value of start is used i.e 0. The output for the same is given below:


5

The output is 5 because tonight substring is starting from 5.

Python String find(): To find a letter between two positions

Below we have an example where we are trying to search a letter between two positions. In this case, it will return the index of the first occurrence of that letter. Let us see:

str1 = 'Titanic is a great movie'
index = str1.find('e', 0, 17)
print(index)


15

The output is 15 because the first occurrence of the letter 'e' is at index 15.

Example to find a character that does not exist in the string using find()

str1 = 'Titanic is a great movie'
index = str1.find('k', 0, 7)
print(index)

In the above example, we are looking for the letter 'k' in Titanic and as you can see it does not exists. The output for the same is given below:


-1

Time For Live Example!

Let us take a look at the live example of Python string find() for a clear understanding of this method:

Summary

In this tutorial, we learned find() method of strings in Python that is used to find a substring in a string or a letter in a string. We also saw its parameters and returned values with some examples and a Live example.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.