Skip to main content

How to find the index of a substring in a string in Python

How to find the index of a substring in a string in Python.

Here is a detailed step-by-step tutorial on how to find the index of a substring in a string in Python:

Step 1: Understand the Problem

The goal is to find the starting index of a substring within a given string. The substring is a smaller sequence of characters that we want to locate within the larger string.

Step 2: Using the find() Method

Python provides a built-in method called find() which can be used to find the index of a substring within a string. The find() method returns the index of the first occurrence of the substring, or -1 if the substring is not found.

Here is the syntax for the find() method:

string.find(substring, start, end)
  • string: The original string in which we want to find the substring.
  • substring: The sequence of characters we are searching for.
  • start (optional): The starting index from where the search should begin. If not specified, it will start from the beginning of the string.
  • end (optional): The ending index till where the search should be performed. If not specified, it will search till the end of the string.

Step 3: Using the index() Method (Alternative)

Alternatively, you can use the index() method to find the index of a substring. The index() method works similar to the find() method, but it raises a ValueError if the substring is not found instead of returning -1.

Here is the syntax for the index() method:

string.index(substring, start, end)
  • string: The original string in which we want to find the substring.
  • substring: The sequence of characters we are searching for.
  • start (optional): The starting index from where the search should begin. If not specified, it will start from the beginning of the string.
  • end (optional): The ending index till where the search should be performed. If not specified, it will search till the end of the string.

Step 4: Examples

Let's see some examples to understand how to find the index of a substring in a string:

Example 1: Using the find() method

string = "Hello, World!"
substring = "World"
index = string.find(substring)
print(index) # Output: 7

Example 2: Using the find() method with start and end arguments

string = "Hello, World!"
substring = "o"
start = 5
end = 10
index = string.find(substring, start, end)
print(index) # Output: 8

Example 3: Using the index() method

string = "Hello, World!"
substring = "World"
index = string.index(substring)
print(index) # Output: 7

Example 4: Using the index() method with start and end arguments

string = "Hello, World!"
substring = "o"
start = 5
end = 10
index = string.index(substring, start, end)
print(index) # Output: 8

Step 5: Handling Substring Not Found

Both the find() and index() methods return -1 or raise a ValueError respectively when the substring is not found. You can check for this condition and handle it accordingly in your code.

Now you have a good understanding of how to find the index of a substring in a string using the find() and index() methods in Python.