Question 223 of 169
PCAP Strings Practice Question
You are a data analyst working with a dataset of customer reviews. Each review is stored as a string in a list. You need to count how many reviews contain the word 'excellent' (case-insensitive). However, the word might appear as 'Excellent', 'EXCELLENT', or even with punctuation like 'excellent!'. The current code uses 'excellent' in review.lower(), but this fails if 'excellent' is part of another word like 'unexcellent'. You need to ensure that only the whole word 'excellent' is counted. Which code modification will correctly count whole word occurrences?
⚠ Common exam trap
Python Institute often tests the distinction between substring matching and whole-word matching, and the trap here is that candidates assume `in` with `split()` or `count()` handles whole words, but they fail to account for punctuation or compound words, leading to incorrect counts.
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
✓
Use re.search(r'\bexcellent\b', review, re.IGNORECASE)
`re.search(r'\bexcellent\b', review, re.IGNORECASE)` uses the `\b` word boundary anchor to ensure that 'excellent' is matched as a whole word, not as part of another word like 'unexcellent'. The `re.IGNORECASE` flag handles case-insensitive matching, covering 'Excellent', 'EXCELLENT', etc. This approach also correctly handles punctuation attached to the word, such as 'excellent!', because the word boundary matches between a word character and a non-word character.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
Use re.search(r'\bexcellent\b', review, re.IGNORECASE)
Why this is correct
The \b word boundary anchors ensure that 'excellent' is matched only when it stands as its own word, not as a substring of a larger token, while the re.IGNORECASE flag makes the match case-insensitive. Because re.search scans the entire string but the boundary restricts the match position, this option correctly finds 'Excellent', 'excellent.', and 'excellent' while rejecting 'unexcellent'. This is the only approach that combines whole-word semantics with case-insensitive matching in a single call.
- ✗
Use 'excellent' in review.lower().split()
Why it's wrong here
This membership test lowercases the review and splits on whitespace, which sounds sensible until punctuation breaks the assumption: a token like 'excellent!' or '(excellent)' retains the punctuation and therefore will not equal the literal string 'excellent'. Split() only tokenizes on whitespace, not on punctuation, so real-world sentences with commas, periods, or exclamation marks produce false negatives. The test also relies on exact whole-token equality, which is brittle and fails on compound forms that regex \b handles flawlessly.
- ✗
Use review.lower().count('excellent') > 0
Why it's wrong here
The count() method returns the number of non-overlapping occurrences of the substring, so the expression count('excellent') > 0 is both a misuse of counting semantics and a source of false positives. For example, the word 'unexcellent' contains the substring 'excellent' inside it, making the condition True even though 'excellent' is not a standalone word. This option ignores lexical boundaries entirely; it operates on raw character sequences and cannot distinguish between a word occurrence and a substring occurrence.
- ✗
Use review.lower().find('excellent') != -1
Why it's wrong here
find() returns the index of the first occurrence of the literal substring anywhere in the string, so it reports success for cases like 'inexcellent' or 'excellent' embedded in a longer token, producing the same false positive as count() but via index arithmetic. The comparison against -1 is a common pattern, but the deeper flaw is that substring matching does not respect word boundaries, so it also matches within inflected forms or compound words. Unlike split(), find() never tokenizes and treats the string as a flat character sequence, making it unsuitable for whole-word searches.
About these practice questions
Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →
Last reviewed: Jun 30, 2026
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.
Question Discussion
Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.
Sign in to join the discussion.