Hit enter after type your search item

The Python string find method – search in strings

The find() function returns the index number of the first occurrence of the given search term in the specified string. If the specified string does not contain the search term, the find() returns -1.

The first occurrence means, if the search term exists twice or more in the source string then it will return the lowest index; I will show you an example in the later part of this tutorial.

If you want to return the highest index of the search term then use the rfind() function. See rfind() example in the later part or by clicking the above link.

Syntax of using the find method

This is how you may use the string find() method:

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

A few main points about the find method are:

  • You may specify the starting index to start searching for the given string.
  • The end index can also be specified.
  • The default value for start parameter is 0 i.e. it starts searching the string from the beginning.
  • The end parameter default value is the length of the string i.e. the given search term will be searched from starting index to the end of the string.
  • You should use the find method only if search term/substring position is required.
  • You should use the in operator for knowing if the sub is substring or not.

See the following section for using the Python find() and rfind methods. I will show an example of using the in operator for searching in a string as well (last example).

An example of find() function with default values

In this find() method example, no values are passed in the find() method. So, the default values will be used for start and end indices to tell the lowest index of the given substring:

string contains

What if substring occurs twice or more?

As mentioned earlier, the find() will return the lowest index if the substring/search term occurrence is two or more in the given string. Have a look in this example:

Python string find

You saw, the source string contains the word ‘method’ twice and find method returned the index of the first occurrence.

An example if substring does not exist

See the return value if the given search substring is not found in the source string. For that, the print function is used to take the user entered substring. The entered substring is checked against the source string by using the find() method.

find index

I ran the program twice; first time I entered the word “hello” and it returned -1. The second time I entered the word “term” and the result is 45. Try running this program in your shell and see how it works!

Using the start and end parameters example

For this example, the search term or substring is searched in a sliced source string. That is, the source string sliced by using the start and end parameters. For that, the following source string is created:

“This is a Python Tutorial. Python is great!”

find start end

In the source string, the first occurrence of the word “Python” is the 10th position. However, as we given the start index as 17 and end 35, so find method returned the index of the second occurrence which is 27.

Getting the highest index example by rfind() method

The only difference between the find() and rfind() method is that the rfind() returns the largest index position of the substring. The rfind method uses the same parameters and returns the index number.

See a demonstration below where a string with “is” word occurs twice. The rfind() method is used to return the index number and see the outcome:

rfind method

The output shows the index position of “is” 39. This is largest.

An example of using in operator

As I mentioned in the above section, Python official website recommends using the find() method for getting the position of sub (substring/search term). If you need to check if the sub is a substring or not in the specified string then use the in operator.

The sub in str will return true if the substring is found in the given string. The sub stands for the search term and str is the source string – where you want to find the sub.

See an example of using the Python in operator below:

The output of the above example is:

Source string: using in operator in Python for testing the membership!

The term ‘in’ exists? True

The term ‘out’ exists? False

You see, as the word ‘in’ exists, it returned as true and ‘out’ as false.

This div height required for enabling the sticky sidebar