Python | Find the Index of First Occurrence of Substring in a String

In Python, the “string” represents the sequence of numbers, alphabet, or any characters that are enclosed in single or double quotes. A “substring” is the slicing of a string as it is the sequence of characters within it.

Multiple operations are performed on a string, like replacing, removing, inserting, etc., using various built-in functions. To determine the index position of the first and last occurrence of a substring in a string can be found using different built-in functions of Python.

This write-up will provide a detailed analysis of how to find the index of the first occurrence of a substring in a string. The approaches discussed in this write-up are shown below:

So let’s begin our guide!

Method 1: Using str.find()

How to install Kali Linux in Window...

The “str.find()” function is used to find the index of the first occurrence of a substring in a given string in Python. The “str.find()” function also finds the index of the substring after the particular index value in a string.

Example 1: Using Default parameter

The following example code shows the basic usage of the “str.find()” function:

Code:

string_value = "Python Guide Provide by its-Linux-Foss"

print('Index of the Given Substring is: ', end=' ')
print(string_value.find("Guide"))

print('\nIndex of the Given Substring is: ', end=' ')
print(string_value.find("Java"))

In the above code:

  • The “string” is initialized in a variable named “string_value”.
  • The “str.find()” accepts a substring and retrieves the index of the first existence of the given substring.
  • The “str.find()” will return “-1” if the targeted substring is not determined in the input string.

Output:

The above output shows the index of the first existence of a substring in an input string “string_value‘.

Example 2: Using Parameter Value

The following example shows the use of “str.find()” by using its parameter value.

Code:

string_value = "Python Guide Provide by its-Linux-Foss Python"

print('Index of the Given Substring is: ', end=' ')
print(string_value.find("Python", 6))

In the above code:

  • The string value is initialized in the program.
  • The “str.find()” takes two argument values inside the parentheses.
  • The first argument indicates the substring that needs to be found in the given substring.
  • The second argument indicates the index value from which the function finds the first substring occurrence position in a given string. We specified “6” as a second parameter, so the find() method will start the search from the sixth index.

Note: You can also initialize the value of the third parameter in the function. The third parameter, “stop” indicates that the function will not find any substring after that index.

Output:

The above output shows that the str.find() function retrieves the index of the substring based on the second parameter.

Method 2: Using str.index()

In the code given below, the “str.index()” is used to determine the index of the first occurrence of a substring in a string.

Code:

string_value = "Python Guide Provide by its-Linux-Foss"

print('Index of the Given Substring is: ', end=' ')
print(string_value.index("Guide"))

print('\nIndex of the Given Substring is: ', end=' ')
print(string_value.index("Java"))

In the above code:

  • The “string_value” variable is initialized in the program.
  • The “str.index()” takes the value of the substring as an argument and retrieves the index of the first substring occurrence in an input Python string.
  •  If the substring value does not exist in the input string, then the “str.index()” function returns a value error.

Output:

The above output shows the index position of the first possible position of a substring found in the given string. But when the substring is not found in the string, it shows the “ValueError”.

Method 3: Using str.rfind() and str.rindex()

In the following examples, the “str.rfind()” and “str.rindex()” function is used to determine the last position of a substring in a given string. These functions find the string from right to left and return the last occurrence. The following code shows the working of this function:

Code:

string_value = "Python Linux Guide Provide by Python Linux"

print('Index of the Given Substring is: ', end=' ')
print(string_value.rfind("Python"))

print('\nIndex of the Given Substring is: ', end=' ')
print(string_value.rindex("Linux"))

In above code:

  • The string value is assigned to a variable named “string_value”.
  • The “rfind()” and “rindex()” are used to find the last occurrence of a substring in a given string by locating it from right to left.

Output:

The last occurrence of “Python” and “Linux” has been located in the above output.

That’s it from this write-up!

Conclusion

The Python functions “str.index()” and “str.find()” are used to determine the first position of a substring within a given string. The “rfind()” and “rindex()” are used to locate the last position of a substring in an input string. The “str.find()” and “str.index()” functions also find the substring after the particular index position in a given string. This write-up presented how to determine the first occurrence index position and last occurrence index position of the substring in a string.