Python String find() Method

The Python find() method searches the given string argument in the subject string and returns the lowest index position of the occurrence of the string within another string.

Syntax:

string.find(substr, start, end)

This find() method accepts three parameters.

substr(required) This substring to be searched in subject string.
start(optional) It is an integer value specifying the index position to start the search. If not specified, the default value 0 is taken.
end(optional) It is an integer value specifying the index position where to end the search. If not specified, -1 value is taken, and mean full string length is searched
Return Value:

The lowest index position of the occurrence of the string within another string. If the given string is not found, -1 is returned.

In the following example, the occurrence of 'i' is searched within the string, and the index always starts with 0. Hence it will return 4 as a result.

			

# Python example of the find() method.

str = "Nothing is impossible!!!"
result = str.find("i")
print(result)

				

Output:

4

If no start and end arguments are assigned.

			

name = 'Tutorials Nation'
result = name.find('Nation')
print("Substring 'Nation' occurrence found at", result)

				

Output:

Substring 'Nation' occurrence found at 10

The following example checks, If the given string is found.

			

name = 'Tutorials Nation'
if name.find('web') != -1:
    print("The substring is found.")
else:
    print("The substring is not found.") 

				

Output:

The substring is not found.