Courseiva

PCEP Practice Question: Data Types, Variables, Basic I/O and Operators

A developer wants to output a variable price with two decimal places using formatting. Which line of code will produce 'Price: $12.50' for price = 12.5?

⚠ Common exam trap

Python Institute often tests the difference between `round()` (which returns a float and may not add trailing zeros) and format specifiers (which control string representation), leading candidates to mistakenly choose Option C thinking it produces two decimal places.

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

✓

print(f'Price: ${price:.2f}')

It uses an f-string with the format specifier `:.2f`, which formats the float `12.5` as a string with exactly two decimal places, producing '12.50'. The f-string then interpolates this into the full string 'Price: $12.50'.

Answer analysis

Option-by-option breakdown

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

  • ✗

    print('Price: $' + price)

    Why it's wrong here

    Concatenating a string with a float raises TypeError, since Python will not implicitly convert the number, and no rounding to two places occurs. It tempts because '+' is the obvious joining operator, and it would work only if price were already a string such as '12.50'.

  • ✓

    print(f'Price: ${price:.2f}')

    Why this is correct

    The f-string's `:.2f` format specifier rounds the float to exactly two decimal places, satisfying the stem's two-decimal requirement. Substituting price = 12.5 yields `12.50`, and the literal `$` inside the string produces the required `Price: $12.50` output precisely.

  • ✗

    print('Price: $' + str(round(price, 2)))

    Why it's wrong here

    round(price, 2) returns 12.5, and str() renders it as '12.5', dropping the trailing zero, so the output lacks two decimal places. round() is for numeric rounding, not fixed-width display; format strings or f-strings with :.2f are required.

  • ✗

    print('Price: $%s' % price)

    Why it's wrong here

    %s inserts the string conversion of the float, yielding 'Price: $12.5' with no trailing zero. The %f conversion with a precision specifier, such as %.2f, is what forces two decimal places; %s is for plain string substitution.

About these practice questions

This PCEP question is part of Courseiva's 482-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 PCEP 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 PCEP exam.