Courseiva
StringsmediumMultiple ChoiceObjective-mapped

PCAP re.sub() Practice Question

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?

⚠ Common exam trap

Candidates may assume only `re.sub()` is correct, but `str.translate()` with explicit mapping and list comprehension with `isdigit()` also achieve the same result. The exam may expect recognition that multiple Python methods can accomplish the same task.

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)

Options B, C, and D all correctly replace all digits in the message with 'X'. Option B uses `re.sub()` with a regex character class to match any digit. Option C uses `str.translate()` with a mapping from each digit to 'X', which works because the mapping explicitly covers all digits. Option D uses a list comprehension with `isdigit()` to conditionally replace digits. Option A is incorrect because `str.replace()` does not interpret character ranges; it would look for the literal string '0-9'. Therefore, three correct approaches exist.

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

    The str.replace method performs literal substring substitution, not pattern matching. It searches for the exact three-character sequence '0-9' and replaces every occurrence of that literal text with 'X'. Individual digit characters in the message remain untouched, so this does not redact any numbers from the log output.

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

    Why this is correct

    This invokes re.sub with the pattern [0-9], a character class that matches exactly one character from the range '0' through '9'. Each matched digit is replaced independently with 'X', so the entire message is scanned and every digit becomes an X. Because re.sub processes the whole string and replaces all non-overlapping matches, this correctly sanitizes all ASCII digits in the message.

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

    Why this is correct

    str.maketrans builds a translation dictionary that maps each character in the first string to the corresponding character in the second string, so '0' maps to 'X', '1' maps to 'X', and so on. The translate method then applies this table to every character in message, replacing each digit with 'X' in a single C-level operation. This is an efficient, purpose-built mechanism for character-to-character replacement and handles all digits correctly.

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

    Why this is correct

    This uses a list comprehension to iterate over each character in message, testing c.isdigit() to decide whether to replace that character with 'X' or keep it unchanged. The isdigit() method returns True for decimal digits, and the resulting list is joined back into a string with an empty separator. It correctly transforms every digit into an X while preserving all other characters and punctuation.

About these practice questions

Courseiva writes every PCAP question from scratch — 169 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 →

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.