Best way of using the Python find method

  • Reading time:5 mins read

What is the Python find method used for?

The Python find() method returns the index of the first occurrence of a substring in another string and returns index -1 in the event that the substring is non-existent.

 

How to use the Python find method?

Using two strings we are going to investigate the occurrence of the substring in the main string named my_string using two different approaches with the same end result.

Once we have both strings in place we now need to use my_string.find(); a method that searches through the string named my_string for the occurrence of any sequence of characters that we will pass as an argument within the parentheses.

This returns an integer that represents the index of the substring  in my_string.

 

 

In the case above since there is no such sequence of characters that exists within the string named my_string it is certain that we will get -1 as the output.

 

Python find

 

Suppose that we now try to find the substring “Learn” within  my_string. In such a case we will certainly get the index 0 as the output.

This is because the substring for sure exists within my_string and its location within the string is at index 0.

 

 

Instead of printing out the raw index, we can leverage the if and else statements to get a more precise response once the search for a substring within a certain string is done.

We can go ahead and say that if my_string.find() does not return the index number that we intended, let us print out the message ‘Not Found’.

Else if the function returns that index number that we intended then return ‘Found’ as the message.

 

 

Now suppose that we try to find a substring that doesn’t actually exist then in that case we will certainly get a different message.

 

Using the “in” Operator instead of Python find()

The in operator and its complement the not in operator allows checking if any value not necessarily a string is a member of a string or any other sequences such as tuples or array lists.

Unlike the Python find() function that returns the index of a substring within a string, the in operator returns a boolean result.

 

Python find

 

The boolean keyword True signifies that the substring under search was found in the other string while a False means otherwise.

Using an if and else statement, we can choose to print out a different message depending on whether the substring or sequence of characters was present or not.

 

 

We can also try out a different substring that actually exists and in this case, we get a different message.

 

 

We can also find out whether or not a small part of a string exists within a string with more characters even though that string is only a small part of that word.

 

 

Furthermore, we can not only search for a small part of a word but also a sequence of characters that do not actually form into a real word.

 

 

If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.