Python index() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In Python, index() searches for a substring and returns the index of the first match. If the text is missing, it raises ValueError.

sub is required. Optional start and end limit the search to a slice. Use find() when absence should return -1 instead.

Syntax

Call index with the substring. Start with this line.

python
pos = text.index("Python")
CallWhat it does
"hello".index("ll")Returns 2.
"hello".index("x")Raises ValueError when missing.

Catch ValueError when the substring might be absent, or switch to find().

Library declarations
python
str.index(sub[, start[, end]]) → int

Parameters

ParameterDescription
subRequired. The substring to search for.
startOptional. Index where the search starts. Default 0.
endOptional. Exclusive end index of the search range.

Return Value

Return ValueDescription
intFirst match index, or ValueError if not found.

Python Examples

Example 1 — Required Substring Index

Get the first match when the substring is expected to exist.

python
text = "CodeToFun"

print(text.index("To"))
print(text.index("o"))  # first "o" only

try:
    print(text.index("Java"))
except ValueError as err:
    print("error:", err)

How It Works

  1. Match "To". Starts at index 4.
  2. First only. Two "o" letters; index returns the first (1).
  3. Missing text raises. "Java" triggers ValueError.
  4. Catch when needed. Use try/except for optional lookups.

Example 2 — Limit Search with start / end

Search inside a slice without building a new string first.

python
text = "one two two three"

print(text.index("two"))
print(text.index("two", 5))

try:
    print(text.index("two", 0, 5))
except ValueError:
    print("not in text[0:5]")

How It Works

  1. Default search. First "two" begins at index 4.
  2. Raise the start. From index 5, the next match is at 8.
  3. Cap the end. text[0:5] has no full "two" → error.
  4. Slice semantics. Bounds behave like slicing.

Example 3 — index() vs find()

Same success path; different failure behavior.

python
text = "Python"

print("find:", text.find("Java"))

try:
    print("index:", text.index("Java"))
except ValueError as err:
    print("index error:", err)

How It Works

  1. find stays quiet. Missing text → -1.
  2. index raises. Same miss becomes a ValueError.
  3. Pick by intent. Required match → index; optional → find.
  4. Fail fast. Raising early can reveal bad data sooner.

Frequently Asked Questions

It returns the first index of a substring, or raises ValueError if missing.
Python raises ValueError: substring not found.
Yes. Fold both sides with casefold() for case-insensitive search.
index() raises when missing. find() returns -1.
No. It only returns an integer.

Next: isalnum()

Test whether a string contains only letters and digits.

isalnum() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful