Python String - Find the index of first occurrence of substring


Python String - Find the index of substring

To find the position of the first occurrence of a substring in a string in Python, you can use the string.find() method. This method returns the index of the first occurrence or -1 if the substring is not found.

index = string.find(substring, start, end)

Here:

  • string: The string to search in.
  • substring: The substring to find.
  • start (optional): The starting index for the search.
  • end (optional): The ending index for the search.

https://youtu.be/98LcXuv2_ow?si=2i8OxLvkqLEk84bg

Examples

1. Find the index of substring in string

In this example, we find the first occurrence of the substring prog in the string.

Python Program

string = 'Python programming. Network programming.'
substring = 'prog'

index = string.find(substring)
print(index)

Explanation:

  1. string = 'Python programming. Network programming.'
  2. substring = 'prog': The substring we are searching for.
  3. string.find(substring): Returns the index of the first occurrence of 'prog', which is 7.

Output:

7

2. Find the index of substring after a specific position

Here, we find the first occurrence of prog after index 12 in the string.

Python Program

string = 'Python programming. Network programming.'
substring = 'prog'
start = 12

index = string.find(substring, start)
print(index)

Explanation:

  1. The search starts at index 12.
  2. string.find(substring, start): Finds the first occurrence of 'prog' starting from index 12, which is at index 28.

Output:

28

Now it has found the index of the substring that occurred for the first time after start position.


3. Check if the substring exists

In this example, we check if a substring exists in the string before finding its index.

Python Program

string = 'Python programming. Network programming.'
substring = 'java'

if substring in string:
    index = string.find(substring)
    print(index)
else:
    print('Substring not found')

Explanation:

  1. substring = 'java': The substring to search for.
  2. if substring in string: Checks if the substring exists in the string.
  3. If the substring exists, find() returns the index; otherwise, it prints 'Substring not found'.

Output:

Substring not found

4. Find all occurrences of a substring

Here, we find all occurrences of the substring in the string.

Python Program

string = 'Python programming. Network programming.'
substring = 'prog'

start = 0
while start < len(string):
    index = string.find(substring, start)
    if index == -1:
        break
    print('Found at index:', index)
    start = index + len(substring)

Explanation:

  1. Initialize start = 0.
  2. Use a while loop to find the substring iteratively.
  3. If find() returns -1, break the loop.
  4. Update start to continue searching after the current occurrence.

Output:

Found at index: 7
Found at index: 28

Summary

In this tutorial, we explored the string.find() method to find the index of the first occurrence of a substring in a string. We also covered:

  • Finding substrings after a specific index.
  • Checking if a substring exists before finding its index.
  • Finding all occurrences of a substring iteratively.

Each example demonstrates a practical application of substring searches in Python.




Python Libraries