Python String Find Method

In python, the string find() method is useful to search the string for specified substring and return the index position of the first occurrence of the specified substring. In case if the specified substring/character is not found, it will return -1.

 

Following is the pictorial representation of a string find() method functionality in python.

 

Python string find method example result

 

If you observe the above diagram, we are trying to find the "Hi" word in the given string “Hi Guest Hi” using the find function in python and it returned the index position of the first occurred specified substring as 0. In python, the index position will always start from 0, so it returned the index position 0.

String Find Method Syntax

Following is the syntax of defining a string find method in python.

 

string.find(string/char, startIndex, endIndex)

If you observe the above syntax, the find() method accepts three parameters (string/char, startIndex, endIndex). Here, startIndex and endIndex parameters are optional.

 

  • string/char - It’s the substring/character you want to search in the string.
  • startIndex - It’s optional and indicates from which index position the search can start. By default, it's 0.
  • endIndex - It’s optional and indicates at which index position the search can end. By default, it's the end of the string.

String Find Method Example

Following is the example of searching the specified substring/character in the given string using the string find() method in python.

 

msg = "Hi Guest hi, welcome to tutlane"

rmsg1 = msg.find("hi")
rmsg2 = msg.find("Tutlane")
rmsg3 = msg.find("to")
rmsg4 = msg.find("Hi")
print("Substring 'hi':", rmsg1)
print("Substring 'Tutlane':", rmsg2)
print("Substring 'to':", rmsg3)
print("Substring 'Hi':", rmsg4)

If you observe the above example, we are searching the required substrings in the given string using find() method without specifying start/end index parameters.

 

When you execute the above python program, you will get the result as shown below.

 

Substring 'hi': 9
Substring 'Tutlane': -1
Substring 'to': 21
Substring 'Hi': 0

As discussed in the earlier chapters, python is a case sensitive language due to that the substring 'hi' was found at index position 9 even though the string starts with 'Hi'. For another substring 'Tutlane', it returned -1 because the specified substring was not found with the same case.

 

We didn’t mention any start/end index positions to search for the specified substrings in the above example. By default, the string find() method will search the complete string for the specified substrings/characters.

 

If you want to limit the search range, you need to mention the start and end index parameter values in the find() method.

 

Following is the example of searching the substrings/characters within the specified index positions range using the find() method.

 

msg = "Hi Guest hi, welcome to tutlane"
rmsg1 = msg.find("to",4,10)
rmsg2 = msg.find("hi",1,15)
rmsg3 = msg.find("Guest", 2, 8)
print("Substring 'to':", rmsg1)
print("Substring 'hi':", rmsg2)
print("Substring 'Guest':", rmsg3)

If you observe the above example, we are limiting the string search by specifying the start and end index positions in find() method.

 

When you execute the above python program, you will get the result as shown below.

 

Substring 'to': -1
Substring 'hi': 9
Substring 'Guest': 3

This is how you can use the string find() method in python to search for the specified substring/character in the given string based on your requirements.