Python String find Method

A visual guide to mastering string search in Python using str.find(). Explore substring detection, case sensitivity, and range-based searching.

Python str.find() method

  • 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 that 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 of this tutorial.

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:

  • You may specify the start index to start searching for the given string.
  • The end index can also be specified.
  • The default value for the 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 the starting index to the end of the string.
  • You should use the find method only if a search term/substring position is required.

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:

#A demo of find() function

src_str = 'Check if string contains the required word?'

sub_index = src_str.find('contains')


print("The source string:" ,src_str)

print("The position of 'contains' word: ", sub_index)

Output:

The source string: Check if string contains the required word?
The position of ‘contains’ word: 16
You should use the in operator for knowing if the sub is substring or not.

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:

src_str = 'Python search string method. The method returns lowest index.'

sub_index = src_str.find('method')

print("The source string:" ,src_str)

print("The index of word 'method': ", sub_index)

Output:

The source string: Python search string method. The method returns lowest index.
The index of word ‘method’: 21

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

An example if a 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.

#A demo of find() function with user input

Your_sub = input("Enter a substring to search?")

src_str = 'Let us see if this string contains the search term you entered or not?'


sub_index = src_str.find(Your_sub)

print("The source string:" ,src_str)

print("The index of word '" ,Your_sub ,"' : " ,sub_index)

find index

I ran the program twice; the first time I entered the word “hello” and it returned -1. The second time I entered the word “term” and the result was 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 is sliced by using the start and end parameters. For that, the following source string is created:

“This is a Python Tutorial. Python is great!”
Python program:
src_str = 'This is a Python Tutorial. Python is great!'

sub_index = src_str.find('Python',17,35)

print("The source:" ,src_str)

print("The index of word 'Python': ", sub_index)

Output:

The source: This is a Python Tutorial. Python is great!
The index of word ‘Python’: 27

In the source string, the first occurrence of the word “Python” is the 10th position. However, as we gave 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:

src_str = 'This is rfind() method. Being Pythonic is cool!'

sub_index = src_str.rfind('is')

print("The source:" ,src_str)

print("The index of word 'is': ", sub_index)

Output:

The source: This is rfind() method. Being Pythonic is cool!
The index of word ‘is’: 39

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

An example of using ‘in’ operator

As I mentioned in the above section, the Python official website recommends using the find() method to get 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 in operator example

src_str = 'using in operator in Python for testing the membership!'

a_bool = 'in' in src_str

b_bool = 'out' in src_str


print("Source string:" ,src_str)

print("The term 'in' exists?" ,a_bool)

print("The term 'out' exists?" ,b_bool)

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.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!