Python String find() Method
While working with string data in Python, sometimes we need to determine the occurrences of the substring from the given string. To do this various file-handling methods are used in Python. For example, finding the first occurrence of the substring from the string is performed using the “string.find()” method in Python. This method will be explained comprehensively in this guide using multiple examples.
This guide discusses the following contents:
What is the Python “string.find()” Method?
-
- Example 1: Retrieving the Substring First Occurrence Using the “string.find()” Method
- Example 2: Retrieving the Substring First Occurrence Using Parameters of the “string.find()” Method
- Example 3: Retrieving the Substring Occurrences Using the “string.find()” and “string.rfind()” Methods
- Example 4: Retrieving the Substring First Occurrence That Does Not Exist
- Example 5: Retrieving the Multiple Substring Occurrences Using the “string.find()” Method
- Example 6: Retrieving the First Occurrences of Space Using the “string.find()” Method
What is the Python “string.find()” Method?
The built-in “string.find()” method is utilized to determine the first occurrences/instances of the given value. It determines the index of the specified substring value from the input string.
Syntax
Parameters
In the above syntax:
-
- The “value” parameter indicates the substring that is required to be searched in the given string.
- The “start” parameter indicates the starting index or position that is used while searching the substring.
- The “end” parameter specifies the ending index or position of the string. By default, it is the last index position value of the provided string.
Return Value
The “string.find()” method retrieves the index of the particular substring in the given string. If the substring does not exist the “-1” value is retrieved to output. It is very similar to the “string.index()” method. However, the “string.index()” method retrieves an error or exception if the value is not present/exists.
Example 1: Retrieving the Substring First Occurrence Using the “string.find()” Method
In this example, we first initialized the string. Next, we used the “string.find()” method multiple times to find the index or location of the specified substring in the input string.
print("Index of Substring 'Welcome':", string1.find('Welcome'))
print("\nIndex of Substring 'Python':", string1.find('Python'))
print("\nIndex of Substring 'Programming':", string1.find('Programming'))
print("\nIndex of Substring 'Guide':", string1.find('Guide'))
The above code retrieves the following output:
Example 2: Retrieving the Substring First Occurrence Using Parameters of the “string.find()” Method
The “string.find()” method can also retrieve the first occurrence of a substring index or position by taking the start and end parameter values. Here, in this code, the “string.find()” method retrieves the first occurrence of the “Python” substring by starting searching from the “5” index position. Similarly, the “string.find()” method retrieves the index of the “Programming” substring from the given string by taking the “5” and “30” as the start and end index values. Lastly, the method is utilized again to retrieve the index position of the “Guide” substring from the provided string.
print(string1.find('Python', 5))
print(string1.find('Programming', 5, 30))
print(string1.find('Guide', 30, 40))
The retrieved index of the given substring value is:
Example 3: Retrieving the Substring Occurrences Using the “string.find()” and “string.rfind()” Methods
In Python, the “rfind()” method and the “find()” method can retrieve the index of the provided substring. The “rfind()” retrieves the highest index from the right while the “find()” method retrieves the index position from the left of the very first index. If the provided substring does not exist in the particular string then the “rfind()” method and “find()” method retrieves the “-1” value. The below snippet shows how both these functions work:
Here, in this code, the “string.find()” method retrieves the index of the first occurrences of the provided substring while the “string.rfind()” method retrieves the highest substring index.
print("Index of 'Guide' using find():", string1.find('Guide'))
print("\nIndex of 'Guide' using rfind():", string1.rfind('Guide'))
print("\nIndex of 'i' using find():", string1.find('i'))
print("\nIndex of 'i' using rfind():", string1.rfind('i'))
The below snippet shows the index returns by the “find()” and “rfind()” method:
Example 4: Retrieving the Substring First Occurrence That Does Not Exist
Here, we find the first occurrence of the substring that is not present in the provided string. First, initialize the substring, then pass the specified substring to the “string.find()” method. The passed substring in our case is not present, so “-1” is retrieved to the output.
print("Index of 'k':", string1.find('k'))
The below output verified that the provided substring does not exist:
We can also use the “string.index()” method to get the index of the provided substring from the provided string. If the substring is not present/exists then the “index()” method retrieves the error or exception:
print("Index of 'k':", string1.index('k'))
Here is the output:
Example 5: Retrieving the Multiple Substring Occurrences Using the “string.find()” Method
To retrieve the multiple substring occurrences from the string the “string.find()” method is used along with the for loop in Python. We first initialized the string with the count and index value. Next, the “for” loop iterates over the range which is created using the length of the string. The “string.find()” method is used inside the loop to retrieve the multiple substring occurrences.
count=0
index_value=0
for i in range(len(string1)):
res = string1.find('Guide',index_value)
if(res!=-1):
index_value = res+1
count+=1
print("Total occurrences are: ", count)
The multiple occurrence value is shown below:
Example 6: Retrieving the First Occurrences of Space Using the “string.find()” Method
The “string.find()” method can also retrieve the index of the “1st” occurrence of space in the provided string. Take this code as an example:
res = string1.find(' ')
print(res)
Here is the output:
Conclusion
In Python, the inbuilt “string.find()” method retrieves the first instances of the provided substring in the specified string. This method retrieves the index if the substring exists, otherwise retrieves the “-1” value. We can also determine the substring from the string within the specified index value using the start and end parameter values. The “rfind()” and “find()” methods can retrieve the index of the substring from the right and left sides of the string. This guide delivered multiple examples of the “string.find()” method.
Source: linuxhint.com