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.
In more detail
Common forms are .2f for a float with two decimals, , for thousands separators and >10 to right-align in ten characters. They change only the text shown; the value itself is not rounded. The same specifiers work in format(), str.format and f-strings.
Example
f"{2/3:.2f}" # "0.67"Related terms
- format() — The built-in function that turns a value into a string using a format specifier, as in format(3.14159, ".2f").
- f-string — A Python string with an f before the opening quote, in which expressions in curly brackets are replaced by their values.
- floating-point rounding error — The tiny inaccuracy in float results caused by storing decimal fractions in binary, such as 0.1 + 0.2 giving 0.30000000000000004.
Taught in
- Exponents, Roots And Float Formats — Hands-on Python: Complete Video Course & Book
- Chapter One In One Sitting — Hands-on Python: Complete Video Course & Book
- Forty Minutes, Thirty Items: How PCEP Is Run — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Sixty Five Minutes, Forty Items: How PCAP Is Run — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Special Methods: __str__, __eq__ And Friends — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep