Courseiva
Strings →easyMultiple Choice

PCAP Strings Practice Question

A developer uses the .index() method on a string to find the position of a substring. If the substring is not found, what exception is raised?

⚠ Common exam trap

The PCAP exam often tests the distinction between `.index()` (raises `ValueError`) and `.find()` (returns `-1`), trapping candidates who confuse the exception type with `IndexError` due to the word 'index' in the method name.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

ValueError

The `.index()` method on a string raises a `ValueError` when the specified substring is not found. This is because `ValueError` is the standard Python exception for cases where a function receives an argument with the correct type but an inappropriate value, such as a substring that does not exist in the target string.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    KeyError

    Why it's wrong here

    KeyError is specifically raised when a mapping such as a dict is accessed with a key that does not exist. Strings are sequence types, not mappings, and str.index() does not use keys; it searches for a substring value within the string. A missing substring is a value-not-found condition, not a missing-key condition, so KeyError can never occur here.

  • ✗

    IndexError

    Why it's wrong here

    IndexError is raised when you try to access an index that is out of range for a sequence, such as my_string[100] on a short string. The .index() method does not access an index directly; it scans the string for a substring and returns the index of the first match. If the substring is absent, no index is even considered, so the failure is not an invalid index access but rather a failed search, which triggers a different exception.

  • ✓

    ValueError

    Why this is correct

    str.index() raises ValueError exactly when the substring argument cannot be found in the string. This is the documented, intentional behavior: ValueError signals that a value (the substring) is not present in the sequence. It matches the convention used by list.index() and tuple.index(), which also raise ValueError when the searched element is missing. Unlike .find(), which returns -1 as a sentinel, .index() chooses to raise so you must either catch the exception or check with 'in' first.

  • ✗

    TypeError

    Why it's wrong here

    TypeError occurs when an operation is applied to an object of an inappropriate type, such as passing an integer as the substring argument to str.index(). In this scenario, the substring is a valid string but simply does not appear in the target string; the argument's type is perfectly valid and the failure is about the value's absence, not about type incompatibility. Therefore, TypeError is wholly unrelated to the missing-substring situation described in the question.

About these practice questions

This PCAP question is part of Courseiva's 421-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.