The index () method in Python is used in the tutorial, And the python tutorial is used in the tutorial.

Source: Internet
Author: User

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

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.