Python String find() Method

You are Here:

Python String find()

The find() method returns the index value of the first occurrence of the substring in the given string within the specified limit.

Note: The find() method is almost the same as the index() method, the only difference is that the index() method raises an exception if the value is not found, whereas the find() method returns -1.


Python Compiler
txt = "Python is simple, Python is great" x = txt.find("Python") print(x)




txt.find(sub, start, end)

Parameter Values

subRequiredSpecifies the substring to be searched.
startOptionalSpecifies the index position to start the search.
Default value is 0.
endOptionalSpecifies the index position to end the search.
Default value is the length of the given string (txt).

Return Value

NumberReturn the lowest index in the string where substring is found.
-1If the substring is not found.

More Examples

Check if position 5 to 32 has the substring "Python":


Python Compiler
txt = "Python is simple, Python is great" x = txt.find("Python", 5, 32) print(x)




In the following example, we will find the difference between the find() and index() methods.

Python Compiler
txt = "Python is simple, Python is great" x = txt.find("world") print(x) y = txt.index("world") print(y)




Hi Developers, we almost covered 90% of String functions and Interview Question on Python with examples for quick and easy learning.

We are working to cover every Single Concept in Python.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author