
Python String find() Method with Examples
What is Python String find()?
Python String find() Method: The Python string find() method is a built-in function used to locate the index of the first occurrence of a specified substring in a given string. If the substring is not found, the method returns -1 instead of raising an exception.
Syntax of Python String find()
string.find(substring, start, end)
Python String find() Method Parameters:
- substring: The substring to search for within the given string.
- start (optional): The position to start the search. Default is 0.
- end (optional): The position to end the search. Default is the length of the string.
Examples of Using Python String find() Method
1. Example of find() with Default Values
mystring = “Welcome to Python Tutorials Site. Best site for Python learning!”
print(“The position of ‘Python’ is at:”, mystring.find(“Python”))
Output:
The position of ‘Python’ is at: 11
2. Example of find() Using Start Argument
print(“The position of ‘Python’ is at:”, mystring.find(“Python”, 20))
Output:
The position of ‘Python’ is at: 42
3. Example of find() Using Start and End Arguments
print(“The position of ‘Python’ is at:”, mystring.find(“Python”, 5, 30))
Output:
The position of ‘Python’ is at: 11
4. Example of find() with Non-Existent Substring
print(“The position of ‘Java’ is at:”, mystring.find(“Java”))
Output:
The position of ‘Java’ is at: -1
Python string rfind() Method
The rfind() method is similar to find(), but it returns the highest index (last occurrence) of the substring.
Example of rfind() Usage:
print(“The position of ‘Python’ using find() :”, mystring.find(“Python”))
print(“The position of ‘Python’ using rfind() :”, mystring.rfind(“Python”))
Output:
The position of ‘Python’ using find() : 11
The position of ‘Python’ using rfind() : 42
Python string index() Method
The index() method works like find() but raises a ValueError if the substring is not found.
Example of index() Usage:
print(“The position of ‘Python’ using index() :”, mystring.index(“Python”))
If substring is not found:
print(“The position of ‘Java’ using index() :”, mystring.index(“Java”))
Output:
ValueError: substring not found
Counting Total Occurrences Using find()
To count the total number of occurrences of a substring, loop through the string and use find() with startIndex.
Example:
my_string = “test string test, test string testing, test string test string”
startIndex = 0
count = 0
for i in range(len(my_string)):
k = my_string.find(‘test’, startIndex)
if k != -1:
startIndex = k + 1
count += 1
print(“The total count of substring ‘test’ is:”, count)
Output:
The total count of substring ‘test’ is: 6
Summary
- The Python find() method is used to locate the first occurrence of a substring in a string.
- The rfind() method finds the last occurrence of the substring.
- The index() method works like find() but raises an exception if the substring is not found.
- Using find() with a loop helps count the total occurrences of a substring.