Python Reference Library →

Python String index() Method – Find Position of a Substring

Let’s look at string index() method – find position of a substring

  1. Notice

    index() returns the first matching substring index. It raises ValueError when no match exists.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
text = 'banana'
result = text.index("a")
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'banana'Object 2 · int1
This step

index() returns the first matching substring index. It raises ValueError when no match exists.

Picture string index() method – find position of a substring

index() returns the first matching substring index. It raises ValueError when no match exists.

Image brief

Alt text
A diagram for Python String index() Method – Find Position of a Substring. Instructions: text = 'banana' result = text.index("a") print(result). Displayed result: 1. The final example bindings are text --> object 1 (str); result --> object 2 (int). object 1: 'banana'; object 2: 1
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python String index() Method – Find Position of a Substring". Teaching focus: index() returns the first matching substring index. It raises ValueError when no match exists. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = 'banana' result = text.index("a") print(result) | v DISPLAYED RESULT 1 FINAL BINDINGS text --> object 1 (str) result --> object 2 (int) object 1: 'banana' object 2: 1 Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python String index() Method – Find Position of a Substring. Instructions: text = 'banana' result = text.index("a") print(result). Displayed result: 1. The final example bindings are text --> object 1 (str); result --> object 2 (int). object 1: 'banana'; object 2: 1

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
text = 'banana'
result = text.index("a")
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'banana'Object 2 · int1
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
1

What do you expect this program to print?

What does this example display?

text = 'banana'
result = text.index("a")
print(result)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

1

Follow the printed values in order. The output is: 1

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

index() returns the first matching substring index. It raises ValueError when no match exists.

index() returns the first matching substring index. It raises ValueError when no match exists.

More examples and reference

Python String index() Method

The index() method in Python is used to find the position of the first occurrence of a substring inside a string. If the substring is not found, it raises a ValueError.

Syntax

string.index(substring, start, end)

Parameters:

  • substring – The string to search for.
  • start – (Optional) The position to start searching from. Default is 0.
  • end – (Optional) The position to stop searching. Default is end of the string.

Returns:

  • The index (integer) of the first match of the substring.

Example 1: Basic Usage

text = "Python is fun"
print(text.index("fun"))
10

Explanation: The word "fun" starts at index 10 in the string.

Example 2: With Start and End Parameters

text = "banana"
print(text.index("a", 2))
3

Explanation: It starts searching from index 2, so it finds the second "a" at position 3.

Difference Between index() and find()

  • index() raises a ValueError if the substring is not found.
  • find() returns -1 instead of raising an error.

Common Error

text = "hello"
print(text.index("z"))  # Raises ValueError
ValueError: substring not found

Use Case: Validate Substring Presence

text = "apple pie"
if "pie" in text:
    print("Found at index:", text.index("pie"))
Found at index: 6

Summary

  • string.index() returns the index of the first occurrence of a substring.
  • It raises a ValueError if not found.
  • Use optional start and end to limit the search range.

Practice Problem

Write a program to find the position of the word "world" in the string "hello world" using index().

text = "hello world"
print("Index of 'world':", text.index("world"))

Expected Output:

6

Next Steps

Try using index() with different substrings and edge cases like empty strings or overlapping matches.

What to remember

Connect what you noticed in the example with the idea below.

Remember

index() returns the first matching substring index. It raises ValueError when no match exists.

Watch out for this idea

index() returns -1 when no match exists.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →