format()
The built-in function that turns a value into a string using a format specifier, as in format(3.14159, ".2f").
In more detail
It uses the same specifiers as a replacement field, so format(x, ".3f") matches "{:.3f}".format(x). It returns a string for display; to change the number itself use round.
Example
format(2/3, ".3f") # "0.667"Related terms
- format specifier — The instructions after a colon inside a replacement field, such as .2f for two decimal places, that control how a value is shown.
- round() — The built-in function that rounds a number to a given number of decimal places, returning a new number.
- format method — The string method that returns a copy of a string with each {} replacement field filled in from its arguments.
Taught in
- Exponents, Roots And Float Formats — Hands-on Python: Complete Video Course & Book