Python Program to get the index of the substring in a string


In Python we can use the find() and index() methods to get the index of the first occurrence of the substring in a string. Python provides various string manipulation functions to modify and access text data. In this article, we will write a program to get the index of the substring in a string.

Method 1: Using the find() method

The find method searches for the specific substring which is passed as a parameter to the function and returns the starting index of the substring. If the substring is not found in the string then the find() method returns -1.

The find() method takes 3 arguments when it is called −

  • Substring − The substring you are searching for in the string

  • Start index(optional) − The index from which you want the find() method to start its search. Its default value is 0 i.e starting index of the string.

  • End index(optional) − The index till which you want the find a () method to keep searching for the substring. Its default value is the length of the string i.e the end index of the string.

Syntax

string.find(substring, start, end)

The string is the string in which you want to find the index of the substring. The start and end index passed as a parameter is optional. It will take the default value when not passed as a parameter.

Example

If we want to find the index of the substring “World” in the string “Hello, World!”. We can do it using the find() method as follows −

string = "Hello, World!"
substring = "World"
index = string.find(substring)
print(index)

Output

7

Method 2: Using the index() method

The index() method returns the index of the first occurrence of the substring in the string. If the substring is not found it will raise a valueError.

The index method also takes three arguments similar to the find() method −

  • Substring − The substring you are searching for in the string

  • Start index(optional) − The index from which you want the find() method to start its search. Its default value is 0 i.e starting index of the string.

  • End index(optional) − The index till which you want the find a () method to keep searching for the substring. Its default value is the length of the string i.e the end index of the string.

Syntax

string.index(substring, start, end)

The string is the string in which you want to find the index of the substring. The start and end index passed as a parameter is optional. It will take the default value when not passed as a parameter.

Example

If we want to find the index of the substring “Point” in the string “Welcome to Tutorial Point” we can use the index() method as follows −

string = "Welcome to Tutorial Point"
substring = "Point"
index = string.index(substring)
print(index)

Output

20

Handling Errors

The index() method raises a valueError and the find() method returns -1 when the substring is not found in the string. To handle this error in the index() method and show the output as “Substring not found” we can use a try-except block to handle this error.

Example

If we want to find the index of a substring “Python” in the string “Welcome to Tutorials Point” then the find() method will return -1 and the index() method will raise a value error as the substring is not in the string. Here is how we can use the try-except block to handle the error for the index function.

string = "Welcome to Tutorials Point"
substring = "Python"
try:
   index = string.index(substring)
except ValueError:
   print("Substring not found")

Output

Substring not found

The find() method will simply return -1 when the substring is not found in the string. So we don't need to handle any error in the case of the find() method.

Example

string = "Welcome to Tutorials Point"
substring = "Python"
index = string.find(substring)
print(index)

Output

-1

Conclusion

In Python, we can get the index of the first occurrence of the substring using the find() and index() methods. Both of these methods take substring, start index(optional), and end index(optional) as arguments. The find() method returns -1 when the substring is not found whereas the index() method raises a valueError when the substring is not found in the string. We can handle the valueError using the try-except block in Python.

Updated on: 17-Apr-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements