{"id":"formatting-decimals","slug":"decimals","title":"Decimals in f-strings","text":"A division can give a number with many decimals, and printing it shows them all:\n\n```python\ntotal = 10\npeople = 3\nshare = total / people\nprint(f\"Each pays {share}\")\n```\n\n```output\nEach pays 3.3333333333333335\n```\n\nAn f-string can say how a value should be shown. After the value inside the curly braces, write a colon and a **format specifier**. `.2f` means \"as a decimal number with 2 decimals\":\n\n```python\ntotal = 10\npeople = 3\nshare = total / people\nprint(f\"Each pays {share:.2f}\")\nprint(f\"Exactly: {share:.5f}\")\nprint(f\"Roughly: {share:.0f}\")\n```\n\n```output\nEach pays 3.33\nExactly: 3.33333\nRoughly: 3\n```\n\nThe number before the `f` is the number of decimals, and the last decimal is rounded: 2/3 with `.2f` is `0.67`. The `f` stands for a **f**loat, a number with a decimal point. The value itself doesn't change: only the way it's shown.\n\nThe specifier adds decimals too, which is handy for prices. A whole number gets zeros after the point:\n\n```python\nprice = 4\ndiscount = 0.5\nprint(f\"{price:.2f} {discount:.2f} {price - discount:.2f}\")\n```\n\n```output\n4.00 0.50 3.50\n```\n\nThe last value shows that a calculation can go inside the braces too, with the specifier after it.\n\n### An f-string is a string\n\nAn f-string doesn't have to go straight to `print`. It makes an ordinary string, which you can store in a variable, join with other strings or return from a function:\n\n```python\ndef receipt_line(item: str, price: float) -> str:\n    return f\"{item}: {price:.2f}\"\n\n\nline = receipt_line(\"Tea\", 2.5)\nprint(line)\nprint(receipt_line(\"Cake\", 3.75) + \" (today's special)\")\nprint(len(line))\n```\n\n```output\nTea: 2.50\nCake: 3.75 (today's special)\n9\n```\n\n`receipt_line` returns the formatted text, and the code that calls it decides what to do with it. `len(line)` shows that `line` is an ordinary string of 9 characters.\n","task":"Fill in the blank with a format specifier, so the program prints the average with two decimals:\n\n```\nAverage: 7.67\n```\n","kind":"fill","seed":"scores = [8, 6, 9]\naverage = sum(scores) / len(scores)\nprint(f\"Average: {average:[[.2f]]}\")\n","solution":"scores = [8, 6, 9]\naverage = sum(scores) / len(scores)\nprint(f\"Average: {average:.2f}\")\n","hints":["A format specifier comes after the colon inside the braces.","Two decimals are `.2f`: a dot, the number of decimals and the letter `f`."],"tests":[{"name":"Output","stdin":"","expect":"Average: 7.67","hidden":false}],"require":[],"forbid":[],"more":[{"task":"Write a program that asks for the amount of a bill and the number of people sharing it, and prints how much each person pays, with two decimals:\n\n```\nBill: 100\nPeople: 3\nEach pays 33.33\n```\n\nThe bill may have decimals, so read it with `float`.\n","kind":"code","seed":"# Split the bill.\n","solution":"# Split the bill.\nbill = float(input(\"Bill: \"))\npeople = int(input(\"People: \"))\nprint(f\"Each pays {bill / people:.2f}\")\n","hints":["Read the bill with `float(input(...))` and the number of people with `int(input(...))`.","Each person pays `bill / people`.","Show it with two decimals: `f\"Each pays {bill / people:.2f}\"`."],"tests":[{"name":"Test 1","stdin":"100\n3\n","expect":"Bill: 100\nPeople: 3\nEach pays 33.33","hidden":false},{"name":"Test 2","stdin":"45.5\n2\n","expect":"Bill: 45.5\nPeople: 2\nEach pays 22.75","hidden":true},{"name":"Test 3","stdin":"12\n4\n","expect":"Bill: 12\nPeople: 4\nEach pays 3.00","hidden":true}],"require":[],"forbid":[]},{"task":"Write the function `price_tag(name: str, price: float) -> str`, which returns a price tag with the price in two decimals. For example, `price_tag(\"Coffee\", 3.5)` returns `'Coffee costs 3.50'`.\n","kind":"code","seed":"def price_tag(name: str, price: float) -> str:\n    pass\n\n\nif __name__ == \"__main__\":\n    print(price_tag(\"Coffee\", 3.5))\n","solution":"def price_tag(name: str, price: float) -> str:\n    return f\"{name} costs {price:.2f}\"\n\n\nif __name__ == \"__main__\":\n    print(price_tag(\"Coffee\", 3.5))\n","hints":["Build the text with an f-string that has `name` and `price` in it.","Write the price as `{price:.2f}` for two decimals, and return the string instead of printing it."],"tests":[{"name":"price_tag(\"Coffee\", 3.5)","stdin":"","call":"price_tag(\"Coffee\", 3.5)\n","expect":"'Coffee costs 3.50'","hidden":false,"returns":true},{"name":"price_tag(\"Bagel\", 2)","stdin":"","call":"price_tag(\"Bagel\", 2)\n","expect":"'Bagel costs 2.00'","hidden":true,"returns":true},{"name":"price_tag(\"Sandwich\", 6.499)","stdin":"","call":"price_tag(\"Sandwich\", 6.499)\n","expect":"'Sandwich costs 6.50'","hidden":true,"returns":true}],"require":[],"forbid":[]}]}