All courses › Programming with Python › f-strings and formatting
f-strings and formatting
An f-string, written f"...", inserts values directly into text using curly braces. After a colon you can give a format spec, such as .2f for two decimals. This keeps printed output tidy and readable.
inserts the value of
with 2 decimals
Symbols
| the value being inserted |
Example
x = 3.14159
f"{x:.2f}" gives "3.14".
.2f rounds to two decimals, it does not just truncate them.← Lists · for loop and range →