Courseiva
StringsmediumMultiple ChoiceObjective-mapped

PCAP Strings Practice Question

A developer writes code to display a floating-point number with exactly two decimal places. Which f-string expression is correct for value = 3.14159?

⚠ Common exam trap

Python exams often test the distinction between width and precision in format specifiers, trapping candidates who confuse `0.2` (width.precision without type) with `.2f` (precision with float type), or who mistakenly use `%` syntax from older Python formatting styles.

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:.2f}"

The format specifier `.2f` in an f-string explicitly instructs Python to format the floating-point number with exactly two digits after the decimal point. The `f` type ensures fixed-point notation, and the precision `.2` controls the number of decimal places. This is the standard way to achieve two-decimal-place output for a float in Python.

Answer analysis

Option-by-option breakdown

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

  • f"{value:0.2}"

    Why it's wrong here

    The format specifier f"{value:0.2}" is invalid because it lacks an 'f' conversion type at the end. Without a type code, the '.2' precision cannot be applied to a floating-point number, and the leading '0' is interpreted as a zero-padding flag that expects a width, making the entire specification malformed for float formatting. In the f-string mini-language, every numeric format must end with a type (such as 'f', 'e', or 'g'); omitting it causes a ValueError or yields unintended string output.

  • f"{value:.2f}"

    Why this is correct

    This is the correct f-string format specifier: the dot ('.') introduces a precision field, '2' is the number of digits to display after the decimal point, and 'f' selects fixed-point notation. For example, f"{3.14159:.2f}" evaluates to '3.14', and Python automatically rounds the value to the requested precision. This matches the requirement to display exactly two decimal places, making it the only valid option among the choices.

  • f"{value:%2f}"

    Why it's wrong here

    The '%' character in f"{value:%2f}" is a leftover from the older %-formatting style (e.g., "%2f" % value) and is not a valid part of an f-string's format specification. In the f-string mini-language, '%' is only a valid type suffix for percentage display (like '.2%'), not a leading modifier, so '%2f' is parsed incorrectly and raises a ValueError. To get two decimal places in an f-string, the correct syntax is '.2f' without any '%'.

  • f"{value:2f}"

    Why it's wrong here

    In f"{value:2f}", the absence of a decimal point before '2' means the '2' is interpreted as a minimum field width, not as a precision. The 'f' type still applies, but with the default precision of six decimal places, so f"{3.14159:2f}" yields '3.141590' rather than '3.14'. This produces more digits than intended and does not satisfy the requirement of displaying exactly two decimal places; the correct specifier needs the dot before the precision value.

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.