Python string find

The Python string find function is used to return the index position of the first occurrence of a specified string. The Python string find function return -1 if the specified string not found.

In this section, we discuss how to write a find string Function in Python Programming with example. The index position in the string find Function start from 0, Not 1.

Python find String Syntax

The syntax of the Python find string function is:

String_Value.find(Substring, Starting_Position, Ending_Position)
  • String_Value: Valid String literal.
  • Substring: String you want to search for.
  • Starting_Position (optional): If you want to specify the starting point (starting index position), then Please specify the index value here. If you omit this parameter, the python string find function considers Zero as a starting position.
  • Ending_Position (optional): If you want to specify the endpoint (Ending index position) then, Please specify the index value here. If you Omit this parameter, string find Function consider the highest index number.

Python find substring in a string Example

The following set of examples help you understand the find string in Python.

# Python find Method Example

Str1 = 'We are abc working at abc company';
Str2 = Str1.find('abc')
print('First Output of a find() method is = ', Str2)

# Performing find() function directly
Str3 = 'Find Tutorial at Tutorial Gateway'.find('Tutorial')
print('Second Output of a find() method is = ', Str3)

# Searching for Not existing Item
Str4 = Str1.find('Tutorial')
print('Third Output of a find() method is = ', Str4)

# Using First Index while finding the String
Str5 = Str1.find('abc', 12)
print('Fourth Output of a find() method is = ', Str5)

# Using First & Second Index while finding the String
Str6 = Str1.find('abc', 12, len(Str1) -1)
print('Fifth Output of a find() method is = ', Str6)

# Using First & Second Index while finding Non existing String
Str7 = Str1.find('abc', 12, 21)
print('Sixth Output of a find() method is = ', Str7)
Python Find

It finds all occurrence of a substring ‘abc’ inside the Str1 using find String Method and prints the output

Str2 = Str1.find('abc')
print('First Output of a find() method is = ', Str2)

If the string find function does not find the specified substring inside the Str1, it returns -1

Str4 = Str1.find('Tutorial')
print('Third Output of a find() method is = ', Str4)

The Python string find function allows us to use the Starting index position of the substring. By specifying the starting index position, we can increase performance.

Str5 = Str1.find('abc', 12)
print('Fourth Output of a find() method is = ', Str5)

The string find function allows us to use Starting and ending indices. By providing the starting and ending index positions, we can increase the performance. The following Python statement starts looking for ‘abc’ from index position 12.

Str6 = Str1.find('abc', 12, len(Str1) -1)
print('Fifth Output of a find() method is = ', Str6)

It is returning -1 because find function start looking from 12 (means the first abc skipped) and end at index position 21. As we all know, the second abc is in position 22.

Str7 = Str1.find('abc', 12, 21)
print('Sixth Output of a find() method is = ', Str7)