Python String find() Method

Python String Find() Method

This is a detailed tutorial of the Python String find() method. Learn to find the first occurrence of a substring in a Python String using this method.

Python String find()

This string method find() is used to find the first occurrence of a substring in the string on which it is applied. It returns the numerical index (position) of the substring in a given string. If the substring provided is not contained in the searched string, this function returns -1.

Syntax

find() Parameters

The string find() method can take upto three arguments. These are defined below.

  • subString. This is a string type argument and it is also the only mandatory parameter. Here you have to pass the substring to lookup in the main string as a string type argument.
  • The other two, start, and end are optional parameters. These two parameters define the lookup window of the string for this method. In other words, the function will lookup for the substring provided as the first argument to this method only between these start and end indexes. By default, when these two parameters are not provided as arguments, the function considers searching the whole string for the first occurrence of the substring.

Note. Make sure you have a look at the examples in order to better understand how you can use these arguments for desired results.

find() Return Value

The String find() method as is used to find the position of the substring in the main string, it returns an integer value.

  • In case, the substring is found in the main string, this method will return its first occurrence from the starting as an integer value. The index starts from 0 from the Left-Hand side.
  • In case, the substring is not found in the main string, this method will simply return the integer value -1.

Note. You can always use the String method rfind() to find out the first occurrence of a substring in a string from the ending side or the Right-Hand side. In other words, the rfind() method is used to find the highest index or the last index of the substring occurrence in a Python String.

Examples

The different examples illustrate the usage of the find() method with or without the various arguments.

Example 1. Use of find() Method without the start and end arguments

The above example illustrates the four different use-cases of the find() method. In the first case, we’re simply finding the numerical index of the first occurrence of the substring in the main string.

In the second case, the substring is not contained in the main string, therefore, this method is returning the numerical index -1. In the third case, I’ve used an If-else statement to check if the substring is contained in the main string or not.

And finally in the fourth case illustrates, even if the string contains multiple instances of the same substring, this method only returns the index for the first instance.

Python String Find() Method Example

Example 2. Using the find() Function only with the start argument

In the first case, it is looking for the substring in the defined window-size of index 21 to the end. Then it is looking for the substring in the window-size of index 63 to the end and similarly, in the third case, the searching window-size is index 81 to the end.

Python Find() Method Start Argument Example

As the last string contains multiple instances of the string “Python”, but as the defined window size starts from the index 18, it returned the index of the third instance of the word “Python” in the main string but it is actually the first instance in the defined searching window of the main string.

Example 3. Passing all arguments to the String method find()

Python String Find() Method Start And End Parameters Example

Example 4. Using Negative Indexes

You can also make use of the negative indexing schemes that we also use in various other Python functions. The following method illustrates this.

Python String Find() Method Negative Indexing Example

Example 5. Use of the rfind() Method

In the following output screenshot, you can clearly see where the String find() method is returning the first occurrence index of the substring, at the same time for the same sub-string the rfind() method is finding the highest index or the index of the last occurrence of the substring.

Python String Rfind() Method Example

I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *