The index () method in Python is used in the tutorial, And the python tutorial is used in the tutorial.
The index () method determines the str string. If the start index beg and end index end give a substring to locate the string or string at the end. This method is the same as the find () method, except that an exception is thrown if the sub-toe is not found.
Syntax
The syntax of the index () method is as follows:
Str. index (str, beg = 0 end = len (string ))
Parameters
- Str -- this option specifies the string to be searched.
- Beg -- this is the start index, which is 0 by default.
- End -- this is the end index. By default, it is equal to the length of the string.
Return Value
Method returns an index. If this str is found, an exception is thrown.
Example
The following example shows how to use the index () method.
#! /Usr/bin/pythonstr1 = "this is string example... wow !!! "; Str2 =" exam "; print str1.index (str2); print str1.index (str2, 10); print str1.index (str2, 40 );
When we run the above program, it will produce the following results:
1515 Traceback (most recent call last): File "test. py", line 8, in print str1.index (str2, 40); ValueError: substring not foundshell returned 1