Cause: When troubleshooting errors, locate the line in the judgment statement: if Testlist.index (' T '):error: Valueerror:substring not found. It turns out that index () and find () are not clearly distinguishable. Find () lookup failure returns 1 without affecting the program running. generally used find!=-1 orFind>-1 as a condition of judgment. 1.index description
The Python Index () method detects if the string contains substrings of STR, and if you specify beg (start) and end (end) ranges, the check is contained within the specified range, just like the Python find () method, except that STR does not report a An exception.
Grammar
Index () method syntax:
Str.Index(Str,Beg=0, End=Len(string))
Parameters
- STR--Specifies the retrieved string
- Beg--Start index, default is 0.
- End--ends the index, which defaults to the length of the string.
return value
Throws an exception if the containing substring returns the starting index value.
Instance
The following example shows an instance of the index () method:
#!/usr/bin/pythonstr1 = "This is string Example....wow!!!"; STR2 = "Exam";p rint str1.index (str2);p rint str1.index (str2, ten);p rint Str1.index (str2, 40);
The result of the above example output is as follows:
1515Traceback (most recent): File "test.py", line 8, in print Str1.index (str2, 40); Valueerror:substring not Foundshell returned 1
2.find description
The Python find () method detects whether the string contains substrings of STR, and returns 1 if the specified beg (start) and end (end) ranges are included in the specified range, if the containing substring returns the starting index value.
Grammar
Find () method syntax:
Str.Find(Str,Beg=0, End=Len(string))
Parameters
- STR--Specifies the retrieved string
- Beg--Start index, default is 0.
- End--ends the index, which defaults to the length of the string.
return value
Returns 1 if the containing substring returns the starting index value.
Copyright NOTICE: Welcome reprint, Reprint please indicate the source http://blog.csdn.net/ztf312/
The difference between index () and find () in Python