Describe index(str, beg=0, end=len(string)) with example
Answers
Explanation:
Parameters. str − This specifies the string to be searched. beg − This is the starting index, by default its 0. end − This is the ending index, by default its equal to the length of the string
Description of index with example:
The process index ( ) states if “str” string follows in string or in a substring of string if initial index beg and ending index end are known.
This method is similar as find ( ), but increases an exception if sub is not found.
Syntax value: str.index(str, beg=0 end=len(string))
For example:
#!/usr/bin/python
str = “this is string example….wow!!!”;
str = “exam”;
print str.index(str);
print str.index(str, 10);
print str.index(str, 40);
OUTPUT
15
15 Trace back (most recent call last):
File “test.py”, line 8, in
Print str.index(str, 40);
Value Error: substring not found shell returned 1