Python .index()

BrandonDusch's avatar
Published Oct 14, 2022
Contribute to Docs

The .index() method returns the index of the first occurrence of a specific value in a tuple. Otherwise, an exception is raised.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

tuple_instance.index(value)

The value is required in order for the .index() to search the tuple_instance.

Example

The following is an example of the .index() method:

my_tuple = ("I'm", "over", "here")
print(my_tuple.index("here"))
# Output: 2

Codebyte Example

The following example throws a ValueError because the specified value does not exist in the tuple (values passed to .index() are case-sensitive):

Code
Output

All contributors

Contribute to Docs

Learn Python on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours