Python index () method
description
Python index () method to detect whether a string contains a substring str, if you specify beg (start) and end (end) range, it is checked whether contained within the specified range, the method and python find () method, in that it If the string str is not an exception will be reported.
grammar
index () method syntax:
str.index(str, beg=0, end=len(string))
parameter
- str - the specified search string
- beg - starting index, the default is 0.
- end - the end of the index, the default is the length of the string.
return value
If you include substring returns the index value at the start, otherwise it throws an exception.
Examples
The following example shows the index () instance method:
#!/usr/bin/python str1 = "this is string example....wow!!!"; str2 = "exam"; print str1.index(str2); print str1.index(str2, 10); print str1.index(str2, 40);
Examples of the above output results are as follows:
15 15 Traceback (most recent call last): File "test.py", line 8, in print str1.index(str2, 40); ValueError: substring not found shell returned 1
Note: In the next few sections, we will detail the use of Python Exception.