Python format() function
Format a value using a format specification.
Syntax
format(value, format_spec="")
Uses the same mini-language as f-strings after the colon: decimals, thousands separators, percentages and alignment.
Parameters
| value | the value |
| format_spec | for example ".2f", ",", ".1%", ">8" |
Return value
str
Example
Code
print(format(3.14159, ".2f")) print(format(1234567, ",")) print(format(0.256, ".1%")) print(format("left", "<10") + "|") print(f"{3.14159:.2f}") # the same inside an f-string
Output
3.14 1,234,567 25.6% left | 3.14
Related
Learn it step by step in the free lesson Strings, with exercises that check your answers.