Type Coercion in Concatenation

Medium ⏱ 12 min 49% acceptance ★★★★★ 4.6
Write a function build_receipt_line(item, qty, price) that returns a single string like "2x Coffee - $9.00", where item is a string, qty is an int, and price is a float. You must explicitly convert non-string values before concatenating — Python will not implicitly convert int/float to str with +.

Examples

Example 1
Input
build_receipt_line("Coffee", 2, 4.5)
Output
'2x Coffee - $9.00'
Explanation

qty * price = 9.0, formatted to 2 decimals.

Example 2
Input
build_receipt_line("Tea", 1, 3.0)
Output
'1x Tea - $3.00'
Explanation

Single unit purchase.

Constraints

  • Total = qty * price, always formatted with exactly 2 decimals.

Topics

Variables & Data Typestype casting

Companies

SwiggyZomato

Hints

Hint 1

An f-string avoids manual str() conversions entirely.

Hint 2

Compute the total first: qty * price.

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