Courseiva
StringshardMultiple ChoiceObjective-mapped

PCAP Strings Practice Question

A team is using f-strings to format a report. They have a variable `value = 0.123456789` and want to display it with exactly 3 significant digits. They write `f"{value:.3g}"`. The output is '0.123'. They expected '0.123'. Is the output correct? If not, what change would produce '0.123'?

⚠ Common exam trap

A common mix-up: candidates confuse 'significant digits' (controlled by `g`) with 'decimal places' (controlled by `f`), leading them to incorrectly choose `.3f` when `.3g` is the correct specifier for significant digits.

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

The output is correct as is.

The format specifier `.3g` in an f-string instructs Python to format the number with 3 significant digits using general format. For `0.123456789`, the first three significant digits are '123', and the general format automatically switches to fixed-point notation when the exponent is small, producing '0.123' exactly as expected.

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 `f"{value:.3s}"`

    Why it's wrong here

    Applying `.3s` to a float is not merely a truncation issue; `float.__format__` does not implement the `s` presentation type, so the expression raises a `TypeError`. The `s` specifier is reserved for strings and would limit a string to three characters, which has nothing to do with numeric precision. Replacing the numeric formatter with this would crash the report rather than produce `0.123`.

  • Use `f"{value:.3f}"`

    Why it's wrong here

    Using `.3f` forces exactly three digits after the decimal point, regardless of the number's magnitude. For `0.123456` the output happens to be `0.123`, but for a value like `0.00123456` it would give `0.001`, discarding the third significant digit while inventing zeros in other magnitude ranges; it is not equivalent to significant-digit formatting. The original `.3g` specifier controls total significant digits, not decimal places.

  • Use `f"{value:.3e}"`

    Why it's wrong here

    The `e` presentation type converts the value to normalized scientific notation with one digit before the decimal point, so `.3e` on `0.123456` produces `1.235e-01`. That changes the form entirely: the leading zero is removed, the exponent becomes explicit, and the printed string is not a plain decimal representation. Significant-digit formatting with `g` would keep the decimal notation because the exponent is not extreme enough to trigger scientific form.

  • The output is correct as is.

    Why this is correct

    Correct—the format spec already in use (`.3g`) rounds to three significant digits and picks fixed-point notation for this magnitude. For `0.123456`, the three significant digits are 1, 2, and 3, and because the adjusted exponent is within the `g` threshold, it prints as `0.123` without an exponent. The output is exactly what the report requires, so no alternative specifier is needed.

About these practice questions

This PCAP question is part of Courseiva's 169-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 →

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.