Python String index() Method

You are Here:

Python String index()

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

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


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




txt.index(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.
exceptionIf 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.index("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