Python String index() Method
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.
Example
Output
Syntax
Parameter Values
Value | Type | Explanation |
---|---|---|
sub | Required | Specifies the substring to be searched. |
start | Optional | Specifies the index position to start the search. Default value is 0. |
end | Optional | Specifies the index position to end the search. Default value is the length of the given string (txt). |
Return Value
Value | Explanation |
---|---|
Number | Return the lowest index in the string where substring is found. |
exception | If the substring is not found. |
More Examples
Check if position 5 to 32 has the substring "Python":
Example
Output
Example
In the following example, we will find the difference between the find() and index()
methods.
Output
Reminder
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.