Courseiva
Strings →mediumMultiple Choice

PCAP Strings Practice Question

A developer needs to replace all occurrences of 'cat' with 'dog' in a string, but only if 'cat' is a whole word (not part of 'category'). Which code achieves this?

⚠ Common exam trap

Python Institute often tests the distinction between simple string methods and regex-based substitution, specifically the need for word boundary anchors (`\b`) to match whole words, which candidates overlook when they assume `replace()` or a plain `re.sub()` pattern is sufficient.

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

✓

re.sub(r'\bcat\b', 'dog', s)

Uses the `re.sub()` function with the regex pattern `r'\bcat\b'`, where `\b` denotes a word boundary. This ensures that only the whole word 'cat' is matched and replaced with 'dog', ignoring cases where 'cat' appears as part of a larger word like 'category'. The `r` prefix makes it a raw string, preventing escape sequence issues.

Answer analysis

Option-by-option breakdown

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

  • ✓

    re.sub(r'\bcat\b', 'dog', s)

    Why this is correct

    The correct solution uses a raw-string regex pattern with word boundary anchors: `\b` at both ends ensures `cat` is only matched as a standalone word, not as a substring inside larger words like `category` or `bobcat`. `re.sub` then replaces every such whole-word occurrence globally, returning a new string with each standalone `cat` changed to `dog`, leaving all other text intact.

  • ✗

    s.replace('cat', 'dog')

    Why it's wrong here

    Using the plain string method `s.replace('cat', 'dog')` performs an unanchored substring replacement across the entire string. It will indiscriminately replace the letters `c-a-t` anywhere they appear, including inside words such as `category`, `catapult`, or `scatter`, producing unintended results when the requirement is to replace only the exact standalone word.

  • ✗

    re.sub('cat', 'dog', s)

    Why it's wrong here

    This regex replacement `re.sub('cat', 'dog', s)` lacks word boundary anchors, so the pattern `cat` matches every substring of three letters `c`, `a`, `t` regardless of surrounding characters. While it does replace all occurrences, it suffers from the same substring-matching flaw as `s.replace('cat', 'dog')`, but via the regex engine—hitting embedded matches in longer words instead of limiting itself to whole words.

  • ✗

    s.replace('cat', 'dog', 1)

    Why it's wrong here

    The `1` as the third argument to `s.replace` imposes a maximum replacement count, so only the first occurrence of the substring `cat` in the string is changed to `dog`. This is insufficient because the requirement explicitly demands replacing *all* occurrences; later `cat` instances—even those that are standalone words—remain unchanged, making the result incomplete.

About these practice questions

One of 421 original PCAP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. 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.