Question 298 of 511
StringsmediumMultiple ChoiceObjective-mapped

Quick Answer

The correct approach is `re.sub(r'[0-9]', 'X', message)`, which uses Python’s `re.sub` function with a character class `[0-9]` to match any single digit and replace it with 'X'. This works because the regex pattern `[0-9]` defines a range that includes all digits 0 through 9, and `re.sub` iterates through the string, replacing every match individually—making it ideal for data sanitization where every digit must be masked. On the Certified Associate Python Programmer PCAP exam, this question tests your understanding of the `re` module’s substitution method versus common pitfalls like using `str.replace()` (which only handles literal strings, not patterns) or incorrect regex syntax like `\d+` (which would replace entire multi-digit sequences with a single 'X', not each digit separately). A frequent trap is confusing `re.sub` with `str.replace` or forgetting that `[0-9]` matches one character at a time. Memory tip: think of `[0-9]` as a “digit-by-digit” scanner—each digit gets its own 'X', so “123” becomes “XXX”.

PCAP Strings Practice Question

This PCAP practice question tests your understanding of strings. Read the scenario carefully and evaluate each option against the stated constraints before committing to an answer. After answering, compare your reasoning against the explanation and wrong-answer breakdown below. Once you have made your selection, read the full explanation to reinforce the concept and understand why each distractor is designed to mislead on exam day.

A logging module receives a message that may contain sensitive data. To comply with data privacy, all digits in the message should be replaced with 'X' before logging. Which approach correctly achieves this?

Question 1mediummultiple choice
Full question →

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'[0-9]', 'X', message)

Option B is correct because `re.sub(r'[0-9]', 'X', message)` uses a regular expression character class `[0-9]` to match any single digit (0-9) and replaces each occurrence with 'X'. This is the standard Python approach for pattern-based string replacement, and it correctly handles all digits without affecting non-digit characters.

Key principle: Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

Answer analysis

Option-by-option breakdown

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

  • message.replace('0-9', 'X')

    Why it's wrong here

    replace() treats the first argument as a literal string, not a character range.

  • re.sub(r'[0-9]', 'X', message)

    Why this is correct

    Uses regex to replace any digit character with 'X'.

    Related concept

    Read the scenario before looking for a memorised answer.

  • message.translate(str.maketrans('0123456789', 'XXXXXXXXXX'))

    Why it's wrong here

    This would also work, but requires manual mapping; re.sub is more concise.

  • ''.join(['X' if c.isdigit() else c for c in message])

    Why it's wrong here

    This works but is less efficient and not a built-in method.

Common exam traps

Common exam trap: answer the scenario, not the keyword

Python Institute often tests the distinction between literal string methods (`str.replace()`) and pattern-based methods (`re.sub()`), trapping candidates who assume `replace()` can interpret character ranges like `'0-9'` as a regex pattern.

Detailed technical explanation

How to think about this question

The `re.sub()` function from Python's `re` module compiles the pattern `[0-9]` into an internal state machine that scans the string linearly, matching each digit and replacing it with 'X' in a single pass. This is more efficient than iterating character-by-character in pure Python (as in Option D) for large strings, because the regex engine is implemented in C. In real-world logging systems, such as those using Python's `logging` module with custom filters, `re.sub()` is commonly used to sanitize sensitive data like credit card numbers or social security numbers before writing to logs.

KKey Concepts to Remember

  • Read the scenario before looking for a memorised answer.
  • Find the constraint that changes the correct option.
  • Eliminate answers that are true in general but not in this case.

TExam Day Tips

  • Watch for words such as best, first, most likely and least administrative effort.
  • Review why wrong options are wrong, not only why the correct option is correct.

Key takeaway

Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

Real-world example

How this comes up in practice

A cloud solutions architect for a retail company is evaluating services for a new workload. The correct answer here reflects best practice for the specific scenario described — not a general cloud recommendation. Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option. Cloud exam questions reward reading the constraint carefully: the same technology can be right or wrong depending on the use case.

What to study next

Got this wrong? Here's your next step.

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

Related practice questions

Related PCAP practice-question pages

Use these pages to review the topic behind this question. This is how one missed question becomes focused revision.

Practice this exam

Start a free PCAP practice session

Short sessions build daily habit. Longer sessions build exam-day stamina. Try a timed session to simulate real conditions.

FAQ

Questions learners often ask

What does this PCAP question test?

Strings — This question tests Strings — Read the scenario before looking for a memorised answer..

What is the correct answer to this question?

The correct answer is: re.sub(r'[0-9]', 'X', message) — Option B is correct because `re.sub(r'[0-9]', 'X', message)` uses a regular expression character class `[0-9]` to match any single digit (0-9) and replaces each occurrence with 'X'. This is the standard Python approach for pattern-based string replacement, and it correctly handles all digits without affecting non-digit characters.

What should I do if I get this PCAP question wrong?

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

What is the key concept behind this question?

Read the scenario before looking for a memorised answer.

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 →

How Courseiva writes practice questions · Editorial policy

Keep practising

More PCAP practice questions

Last reviewed: Jun 30, 2026

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.

Loading comments…

Sign in to join the discussion.

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.