Format Currency with F-Strings

Easy ⏱ 8 min 77% acceptance ★★★★☆ 4.3
Write a function format_price(amount) that returns amount formatted as a string with exactly two decimal places and a leading $, e.g. 9 becomes "$9.00". Use an f-string with a format spec, not manual string concatenation.

Examples

Example 1
Input
format_price(9)
Output
'$9.00'
Explanation

Integer padded to two decimals.

Example 2
Input
format_price(19.5)
Output
'$19.50'
Explanation

One decimal digit padded to two.

Constraints

  • `amount` is an int or float, always non-negative.

Topics

Variables & Data Typesf-strings

Companies

WalmartAmazon

Hints

Hint 1

f"${amount:.2f}" does the formatting in one step.

Hint 2

The :.2f spec always shows two digits after the decimal point.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.