0Pricing
Python Academy · Lesson

Format Specifiers

Control width, precision, and alignment.

The format specifier

Inside an f-string or str.format, a colon introduces a format specifier that controls how a value is displayed: {value:spec}.

pi = 3.14159
print(f'{pi:.2f}')

Controlling decimal places

.Nf formats a float with exactly N digits after the decimal point, rounding as needed.

value = 2.0 / 3.0
print(f'{value:.3f}')
print(f'{value:.0f}')

All lessons in this course

  1. f-string Expressions
  2. Format Specifiers
  3. Format Mini-Language
  4. Template Strings and str.format
← Back to Python Academy