PCAP Strings Practice Question
A developer is working with a string `text = "The quick brown fox jumps over the lazy dog"`. They want to create a new string where every occurrence of the word 'the' (case-insensitive) is replaced with 'a'. However, they must not replace 'the' when it is part of another word, such as in 'then' or 'there'. Which approach correctly achieves this?
⚠ Common exam trap
The trap here is assuming that simple string replace with case conversion or multiple replace calls can handle whole-word matching, when in fact word boundaries require regular expressions.
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
✓
import re; re.sub(r'\bthe\b', 'a', text, flags=re.IGNORECASE)
To replace a whole word case-insensitively, regular expressions with word boundaries are needed. The pattern \bthe\b matches 'the' only when it is a complete word, and the re.IGNORECASE flag makes it case-insensitive. The other methods either replace substrings within words, lose the original casing, or do not handle case variations properly. The correct approach preserves the rest of the string and only substitutes standalone occurrences.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
text.lower().replace('the', 'a')
Why it's wrong here
This converts the entire string to lowercase, then replaces all occurrences of 'the' with 'a'. It would also replace 'the' inside words like 'then' and 'there', turning them into 'an' and 'are'. Additionally, it loses the original casing of other words, which is not desired. So it fails both the whole-word requirement and the case preservation.
- ✗
import re; re.sub('the', 'a', text, flags=re.IGNORECASE)
Why it's wrong here
This uses re.sub without word boundaries. It will match 'the' anywhere, including inside 'then' and 'there', because the pattern is not anchored to word boundaries. The re.IGNORECASE flag makes it case-insensitive, but it still lacks the \b assertions. So it fails to avoid replacing substrings within larger words.
- ✓
import re; re.sub(r'\bthe\b', 'a', text, flags=re.IGNORECASE)
Why this is correct
The regular expression uses word boundaries (\b) to match 'the' only when it appears as a whole word. The re.IGNORECASE flag makes it case-insensitive, so 'The' and 'the' are both matched. This correctly replaces only the standalone word 'the' with 'a', leaving words like 'then' or 'there' unchanged. The result would be 'a quick brown fox jumps over a lazy dog' (with 'The' replaced by 'a' and 'the' replaced by 'a').
- ✗
text.replace('the', 'a').replace('The', 'a')
Why it's wrong here
This replaces all instances of 'the' and 'The' regardless of word boundaries. It would replace 'the' in 'then' and 'there', and also in 'The' at the start. It does not handle case-insensitive matches like 'THE' if present, and it does not respect word boundaries. So it incorrectly modifies parts of other words.
Go deeper
Related to this 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 →
JA
Written and reviewed by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
Last reviewed September 2026 · checked against the official Python Institute exam blueprint
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.