Generative Data Intelligence

Python Find Method

Date:

Let us learn what is Python find method and how to implement the Python string find() function in multiple ways with examples, explanation and much more.

What is Python Find Method?

The find() method returns the index or position of the substring passed within the method arguments if the substring is found. If the substring isn’t found, then the find() method returns -1.

In other words, if you want to find a Substring within a String, the find() method helps you accomplish this task. It provides you with checking if the String contains the substring or not.

The Python string Find() method works only with Strings and not with Integers, Decimals, etc. This is one of the functions to operate with Python Strings.

Syntax

  • Original_String: This string is the original string wherein you may want to find the position of the occurrence of the Sub_String.
  • Start: This parameter indicates the starting point or the lower limit within the Original_String. This is an optional parameter. If the start parameter is blank, it defaults to 0.
  • End: This parameter indicates the endpoint or the upper limit within the Original_String. This is an optional parameter. If the end parameter is blank, it defaults to the length of the Original_String.

Important Pointers:

  • The Start and End parameters can be either positive or negative.
  • The find() method accepts only String and Integer values; float values will throw an error.
  • The Start and End are optional parameters.

Implement Python Find Method with Examples

Let us now look at the different examples of how the find() function can be implemented in Python.

 

Note: This Python program is compiled with Python 3.7.2 Shell on Microsoft Windows 10 OS.

Example 1

The Python string find() function only uses the Sub_String here in this example. The string2 occurs at Index position 15, and hence, the method returns 15. The start and end arguments are optional and hence start = 0 and end = strlen(string1).

Example 2

Since the string2 is not present in the string1, the function returns -1.

Example 3

 

In this example, we’re passing all the arguments to the method, and it returns the index value. The numbers could be negative numbers too but not decimals.

If you have any doubts about Python Find method, let us know about it in the comment section.

More Python Programs

Source: https://www.codingalpha.com/python-find-method/

spot_img

Latest Intelligence

spot_img

Chat with us

Hi there! How can I help you?