Courseiva
Strings →mediumMultiple Choice

PCAP Strings Practice Question

Which of the following is the correct way to format a string to include a variable value with two decimal places in Python?

⚠ Common exam trap

The Python Institute often tests the distinction between the width specifier (e.g., `:2f`) and the precision specifier (e.g., `:.2f`), so candidates mistakenly choose Option D thinking `:2f` means two decimal places, when it actually sets a minimum field width of 2 characters.

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}"

Uses the correct f-string format specifier `:.2f`, where `f` stands for fixed-point notation and `.2` specifies two decimal places. This is the standard Python syntax for formatting a floating-point number to two decimal places within an f-string.

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

    Why this is correct

    Correct. The format specifier `.2f` is a dot (precision marker) followed by `2` (the number of digits) and the fixed-point type `f`. This tells Python to render `value` as a float rounded to exactly two decimal places, so 3.14159 becomes `3.14`. This is the official way to force a fixed number of digits after the decimal point in f-string formatting.

  • ✗

    f"{value:.2}"

    Why it's wrong here

    Incorrect because omitting the type suffix `f` causes Python to fall back to the default `g` format. With `g`, the precision `2` is interpreted as two significant digits, not two digits after the decimal point; for instance, 12.345 becomes `12` rather than `12.35`. To display a fixed number of decimal places, you must explicitly include a float type such as `f` after the precision.

  • ✗

    f"{value%:.2f}"

    Why it's wrong here

    Incorrect because the percent sign appears before the colon, so Python parses `value%` as the expression part of the f-string and attempts to evaluate a modulo operation with no right-hand operand, producing a `SyntaxError`. To display a percentage, the `%` must come after the precision as the type suffix, e.g., `f"{value:.2%}"`, which multiplies the value by 100 and appends a percent sign.

  • ✗

    f"{value:2f}"

    Why it's wrong here

    Incorrect because the digit `2` is not preceded by a dot, so the format spec parser treats it as a minimum field width rather than a precision. In `2f`, `width=2` and `type=f`, meaning the value is printed with the default six decimal places and only padded to a minimum width of two characters—not rounded to two decimals. A precision must be written with a leading dot, as in `.2f`.

About these practice questions

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