PCAP Strings Practice Question
Which TWO string methods raise an exception when the searched substring is not found?
⚠ Common exam trap
Python Institute often tests the distinction between methods that return -1 (find, rfind) versus those that raise an exception (index, rindex), and the trap is that candidates confuse rfind() with rindex() because both perform a right-to-left search.
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
✓
rindex()
(rindex()) is correct because the rindex() method, like index(), raises a ValueError exception when the searched substring is not found. This is in contrast to rfind() and find(), which return -1 instead of raising an exception.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
rfind()
Why it's wrong here
The rfind() method searches the string from right to left and returns the highest index where the substring is found, or -1 when there is no match. It never raises an exception for a missing substring; instead, the -1 return value serves as a sentinel that callers can check explicitly. Therefore, rfind() is not one of the methods that signal a failed search with an exception.
- ✗
find()
Why it's wrong here
Unlike the index() family, find() treats a failed search as an ordinary result: it returns the lowest index where the substring occurs, or -1 if the substring is absent. The method is designed for the common pattern of checking `if s.find(x) != -1` without needing exception handling. Because it returns -1 rather than raising an exception, find() cannot be selected as a method that raises on a missing substring.
- ✓
rindex()
Why this is correct
rindex() performs a right-to-left search but still returns the index of the match as measured from the left end of the original string. If the substring is not present, rindex() raises a ValueError, which is exactly the exception behavior that makes it a correct answer. It mirrors index() in its error semantics, differing only in the search direction.
- ✗
count()
Why it's wrong here
The count() method returns the number of non-overlapping occurrences of a substring, and when the substring is not found, it simply returns 0. Since zero is a valid and meaningful count, there is no exceptional condition to report, so count() never raises an exception for a missing substring. This sentinel-based design makes count() incompatible with the question's request for methods that raise on a failed search.
- ✓
index()
Why this is correct
index() raises a ValueError when the requested substring is not found, which is the defining behavior that separates it from find(). The exception is thrown regardless of how many characters are in the original string, forcing the caller to handle the absence explicitly. Because index() uses an exception rather than a sentinel return value, it is one of the two correct methods for this question.
Go deeper
Related to this question
About these practice questions
Courseiva writes every PCAP question from scratch — 421 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
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.