I am sure in your programming career you must have performed search operation a couple of times. If so, then you will agree with me when I say that writing a search function from scratch is a huge time-consuming activity. In such an instance, a built-in function like string method Find ( ) works like a charm. Because of that, this tutorial is dedicated to Python String Methods – Find ( ).
In this tutorial, you will learn –
What is Python String Method –Find ( ) and how to use it using a very simple example in Python Programming.
As for the better understanding of this topic a working knowledge of Python String will be required!
The suggested reading for this tutorial.
Single Line strings in Python
Multi-Line strings of Python.
What is String method Find ( ) in Python Programming?
‘Find’ is a built-in string method of Python library. As the name suggests this string method performs a search operation on the base string and tries to find the specified sub-string in the same.
The Syntax of String method Find ( )
Find(search_string, startIDX, endIDX)
The syntax is pretty much the same as the startswith and endswith string. But still, let me quickly explain it to you.
The function call starts with its name which is ‘find’ in this case. Similar to other Python string methods that we have discussed, the function find also accepts three arguments. These are
Search_string– As said earlier this is the string which will get searched in the base string.
StartIDX– start index is an integer value which specifies the index number from where the interpreter will start the search.
EndIDX – End index is again an integer value which specifies the index number where the interpreter ends the search.
The Default Values of the StartIDX and EndIDX.
Except for the first parameter, which is search_string, the other two are optional. This is to say, if you choose not to specify any of the optional parameters then the interpreter will use their default values. For example, if you do not specify the start index then the interpreter will use the default value which is 0.
Similarly, if you do not specify the end index then the interpreter will assign the default value to it, which is the last index number of the base string with which you are using the function.
Examples of String method Find ( ) in Python Programming.
To explain the concept of string methods Find ( ) I will be doing three examples. Moreover, in the examples, I will demonstrate each of the three parameters which we discussed earlier in the syntax of the find method.
Base and search string – the prerequisites of String Method Find
To clear the confusion, let me tell you, for the demonstration, we will be using two strings. First will be the base_string, this will be our main string onto which we will perform the search. The second string will be the find_string. In addition, this will be the string which we will try to find in our base string.
Meanwhile, do comment and let me know what are your ‘Base and search strings’ for the example!
So, for the demonstration, our base string will be- I am busy learning Python with Rebellion Rider! And, the find string will be “Rebellion Rider”. Using this information we will create a few examples.
Example 1: Python String Methods – Find (search_string)
In the first example, we will call the find ( ) method using only the first parameter which is ‘search_string’. The first parameter is the only mandatory parameter in the list. As a result, it is a must for us to specify some value for it.
base_string = 'I am busy learning Python with Rebellion Rider!' search_string = 'Rebellion Rider' print ( base_string.find(search_string) );
Let’s break down this code and see what we did here.
Statement 1: In the first statement I declared a variable ‘base_string’ and then I stored our base string into it.
Statement 2: In the second statement I again declared another variable ‘search_string’. As a rule, this variable is holding the string which we want to search in our base string.
Statement 3: The third statement is where we call our string methods find ( ). To call the same we used the dot notation. By the way, in the dot notation, we first have to write the name of the string object. Which in our case is the name of the variable ‘base_string’ which is holding our main string. In addition to that we have a dot operator and then the function call with the proper parameters.
Moreover, you can see here I have called the string method Find ( ) using only the first parameter. In this case, the value of that parameter is ‘Rebellion Rider’.
What will happen when we execute the code?
When the interpreter executes the code it will take the string, which is stored in the string object, and perform the find operation on it. In particular, it will try to find the sub-string which we have passed as the parameter of the method.
Now that I have explained to you the entire code, you must be wondering what will be the output of the string methods Find ( ) of Python Programming?
So what does string method Find of Python returns?
If the search is successful then it will return the lowest index number of the base string where the search string is found. On the other hand, if the search is unsuccessful then the string method Find ( ) of Python Programming return -1.
By the way, I have demonstrated the unsuccessful search in detail in the video. You can check it out here.
Hey, if you learn faster by watching the video then look no further! I have done a brief video tutorial explaining the topic. Check it out now.
Example 2: Python String Methods – Find (search_string, startIDX)
In the second example, I will show you how to call string methods Find ( ) of python programming using search string and start index parameter. Generally, using the second parameter start index you can tell your interpreter from where the search should start.
Whatever value you will set for your start index the interpreter will start the search from that index number in the base string.
For instance, let’s say I want the interpreter to start the search of the search_string which in our case is ‘Rebellion Rider’ from index number 31 in the base string. Therefore, in order to do that, I will set 31 as the startIDX in the function call. Like this
base_string = 'I am busy learning Python with Rebellion Rider!' search_string = 'Rebellion Rider' print ( base_string.find(search_string, 31) );
Except from added value for starIDX in the function call which is right there in statement 3, everything is pretty much the same.
Subsequently, we have set the value for the first two parameters and skipped the third argument. In this case, the interpreter will set the default value for the third argument endIDX.
This time when the interpreter will execute the code, it will start searching for the string ‘Rebellion Rider’ from the index 31 in the base string instead of index 0 as it did in example 1. If it finds the search string at that index then it will return the lower index number otherwise it will return -1.
Example 3: Python String Methods – Find (search_string, startIDX, EndIDX)
In the third example, we will set the value for all three parameters of the string method Find ( ) in Python Programming.
Furthermore, using the startIDX and endIDX you can narrow down the search domain and get more control of it. As a rule, the values of startIDX and endIDX together set a window for the search. That way the interpreter does not need to traverse the entire string to find a substring.
For example, let’s say I want my Python Interpreter to search for the string ‘Rebellion Rider’ in the base string, but only between the index number 31 and 40. Therefore, the code for that will be—
base_string = 'I am busy learning Python with Rebellion Rider!' search_string = 'Rebellion Rider' print ( base_string.find(search_string, 31, 40) );
Now I want you to copy the code and execute it. Also, do let me know in the comment section if the search is successful or not.
Hope this will explain the concept of string methods Find of Python Programming to you. If you still have any doubts then feel free to send me a message on my Facebook or Twitter.
Thanks and have a great day!