Courseiva
Strings →hardMultiple Choice

PCAP Strings Practice Question

A developer needs to format a floating-point number 123.456789 with exactly 2 decimal places and a width of 10 characters, right-aligned. Which format specifier accomplishes this?

⚠ Common exam trap

The PCAP exam often tests the distinction between `f`, `g`, and `e` format types, and the trap here is that candidates confuse the precision meaning (decimal places vs. significant digits) or forget that width must be explicitly specified for padding.

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

✓

:10.2f

The format specifier `:10.2f` combines a total width of 10 characters with exactly 2 decimal places for a floating-point number, right-aligned by default. The `f` type ensures fixed-point notation, and the width of 10 includes the decimal point and digits, padding with spaces on the left.

Answer analysis

Option-by-option breakdown

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

  • ✗

    :.2f

    Why it's wrong here

    The `.2f` specifier sets only the precision—two digits after the decimal point—but omits the minimum field width. As a result, the formatted value is exactly as long as the number itself (e.g., `1234567.00` is 10 characters), so if the integer part grows or shrinks, the column width changes and no padding is enforced. This does not guarantee the required 10-character width that a width component would provide.

  • ✗

    :10.2g

    Why it's wrong here

    With `g`, the precision value represents total significant digits, not digits after the decimal point, and trailing zeros are stripped. For the value `1234567`, `:10.2g` produces something like ` 1.2e+06` (width 10), which uses scientific notation and only one significant digit after the leading mantissa digit. Therefore it does not display a fixed-point number with exactly two decimal places, so it fails the formatting requirement.

  • ✗

    :10.2e

    Why it's wrong here

    The `e` type always forces scientific notation, placing exactly one digit before the decimal point and an exponent after it. For `1234567`, `:10.2e` yields ` 1.23e+06`, which does show two digits in the fraction but is not a fixed-point decimal representation of the original value. Since the requirement is fixed-point formatting, this specifier changes the form of the number and is incorrect.

  • ✓

    :10.2f

    Why this is correct

    The specifier `:10.2f` is the correct choice: `10` sets a minimum field width of 10 characters, `.2` requires exactly two digits after the decimal point, and `f` selects fixed-point notation with no exponent. When applied to `1234567`, it produces `1234567.00`, which is exactly 10 characters (7 integer digits, the decimal point, and 2 fractional digits) and satisfies the width and precision requirements. If the integer part were shorter, leading spaces would pad the output to the specified width.

About these practice questions

One of 421 original PCAP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. 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.