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.
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:
string = 'Python programming. Network programming.'
substring = 'prog'
: The substring we are searching for.string.find(substring)
: Returns the index of the first occurrence of'prog'
, which is7
.
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:
- The search starts at index
12
. string.find(substring, start)
: Finds the first occurrence of'prog'
starting from index12
, which is at index28
.
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:
substring = 'java'
: The substring to search for.if substring in string
: Checks if the substring exists in the string.- 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:
- Initialize
start = 0
. - Use a
while
loop to find the substring iteratively. - If
find()
returns-1
, break the loop. - 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.