The difference between index () and find () in Python

Source: Internet
Author: User
Tags ranges

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.