Courseiva
StringsmediumMultiple ChoiceObjective-mapped

PCAP Strings Practice Question

A developer generates a report where numbers must be right-aligned in a 10-character column using f-strings: f'{value:>10}'. However, some values may be None, causing a TypeError. Which is the most robust way to handle None values without affecting other falsy values like 0?

⚠ Common exam trap

The PCAP exam often tests the distinction between identity checks (`is None`) and truthiness checks (`or`, `if value`) to catch candidates who assume all falsy values should be treated equally, especially when `0` is a valid numeric value that must be preserved.

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

f'{value if value is not None else "N/A":>10}'

It uses an explicit identity check (`value is not None`) to distinguish `None` from other falsy values like `0` or empty strings. This ensures that `0` is still right-aligned as a number, while `None` is replaced with the string `"N/A"` before formatting. The f-string then applies the `>10` alignment specifier to the resulting value.

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 str.format() with a conditional for the format spec

    Why it's wrong here

    Using str.format() with a conditional does not remove the need to explicitly check for None—you still must embed an expression like `value if value is not None else 'N/A'` inside the format call. It is also more verbose than an f-string and offers no performance or readability benefit for this task. The format specification itself cannot conditionally substitute a placeholder based on the value's type or state.

  • f'{value or "N/A":>10}'

    Why it's wrong here

    The expression `value or 'N/A'` uses the logical OR operator to test truthiness rather than identity, so any falsy value—including 0, 0.0, '', or []—will be replaced by 'N/A'. In a numeric report, 0 is a valid measurement and must not be treated the same as a missing value. This option would silently corrupt real data by replacing valid zeroes with a string.

  • f'{value if value is not None else "N/A":>10}'

    Why this is correct

    This option uses a conditional expression that explicitly tests identity with `is not None`, meaning only `None` triggers the fallback while preserving all other values, including 0 and empty strings. The f-string then applies the `:>10` format spec to the selected result, right-aligning either 'N/A' or the numeric value in a 10-character field. It is the only choice that correctly distinguishes a missing sentinel from legitimate falsy data.

  • Wrap the f-string in a try-except block

    Why it's wrong here

    Wrapping the f-string in a try-except block is ineffective because evaluating `f'{value:>10}'` with a `None` value does not raise an exception; `None` is simply converted to the string 'None'. To make this approach work you would have to deliberately raise an exception yourself, which is convoluted and obscures the clear intent of the fallback. It also incurs the overhead of exception handling for what should be a straightforward conditional substitution.

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.