Next / Previous / Contents / TCC Help System / NM Tech homepage

9.4.4. The spec part

After the name and conversion parts of a format code, you may use a colon (“:”) and a format specifier to supply more details about how to format the related value.

Here is the general form of a format specifier.

":" [[fill] align] [sign] ["#"] ["0"] [width] [","] ["." prec] [type]
fill

You may specify any fill character except “}”. This character is used to pad a short value to the specified length. It may be specified only in combination with an align character.

align

Specifies how to align values that are not long enough to occupy the specified length. There are four values:

< Left-justify the value. This is the default alignment for string values.
> Right-justify the value. This is the default alignment for numbers.
^ Center the value.
= For numbers using a sign specifier, add the padding between the sign and the rest of the value.

Here are some examples of the use of fill and align.

>>> "{:>8}".format(13)
'      13'
>>> "{:>8}".format('abc')
'     abc'
>>> "{:*>8}".format('abc')
'*****abc'
>>> "{:*<8}".format('abc')
'abc*****'
>>> "{:>5d}".format(14)
'   14'
>>> "{:#>5d}".format(14)
'###14'
>>> "{:<6}".format('Git')
'Git   '
>>> "{:*<6}".format('Git')
'Git***'
>>> "{:=^8}".format('Git')
'==Git==='
>>> "{:*=-9d}".format(-3)
'-*******3'
sign

This option controls whether an arithmetic sign is displayed. There are three possible values:

+ Always display a sign: + for positive, - for negative.
- Display - only for negative values.
(one space) Display one space for positive values, - for negative.

Here are some examples of use of the sign options.

>>> '{} {}'.format(17, -17)
'17 -17'
>>> '{:5} {:5}'.format(17, -17)
'   17   -17'
>>> '{:<5} {:<5}'.format(17, -17)
'17    -17  '
>>> '{:@<5} {:@<5}'.format(17, -17)
'17@@@ -17@@'
>>> '{:@>5} {:@>5}'.format(17, -17)
'@@@17 @@-17'
>>> '{:@^5} {:@^5}'.format(17, -17)
'@17@@ @-17@'
>>> '{:@^+5} {:@^+5}'.format(17, -17)
'@+17@ @-17@'
>>> '{:@^-5} {:@^-5}'.format(17, -17)
'@17@@ @-17@'
>>> '{:@^ 5} {:@^ 5}'.format(17, -17)
'@ 17@ @-17@'
"#"

This option selects the “alternate form” of output for some types.

  • When formatting integers as binary, octal, or hexadecimal, the alternate form adds “0b”, “0o”, or “0x” before the value, to show the radix explicitly.

    >>> "{:4x}".format(255)
    '  ff'
    >>> "{:#4x}".format(255)
    '0xff'
    >>>  "{:9b}".format(62)
    '   111110'
    >>> "{:#9b}".format(62)
    ' 0b111110'
    >>> "{:<#9b}".format(62)
    '0b111110 '
    
  • When formatting float, complex, or Decimal values, the “#” option forces the result to contain a decimal point, even if it is a whole number.

    >>> 
    "{:5.0f}".format(36)
    '   36'
    >>> "{:#5.0f}".format(36)
    '  36.'
    >>> from decimal import Decimal
    >>> w=Decimal(36)
    >>> "{:g}".format(w)
    '36'
    >>> "{:#g}".format(w)
    '36.'
    
"0"

To fill the field with left zeroes, place a “0” at this position in your format code.

>>> "{:5d}".format(36)
'   36'
>>> "{:05d}".format(36)
'00036'
>>> "{:021.15}".format(1.0/7.0)
'00000.142857142857143'
width

Place a number at this position to specify the total width of the displayed value.

>>> "Beware the {}!".format('Penguin')
'Beware the Penguin!'
>>> "Beware the {:11}!".format('Penguin')
'Beware the Penguin    !'
>>> "Beware the {:>11}!".format('Penguin')
'Beware the     Penguin!'
","

Place a comma at this position in your format code to display commas between groups of three digits in whole numbers.

Note

This feature was added in Python 2.7.

>>>  "{:,d}".format(12345678901234)
'12,345,678,901,234'
>>> "{:,f}".format(1234567890123.456789)
'1,234,567,890,123.456787'
>>> "{:25,f}".format(98765432.10987)
'        98,765,432.109870'
"." precision

Use this part to specify the number of digits after the decimal point.

>>> from math import pi
>>> "{}".format(pi)
'3.141592653589793'
>>> "{:.3}".format(pi)
'3.14'
>>> "{:25,.3f}".format(1234567890123.456789)
'    1,234,567,890,123.457'
type

This code specifies the general type of format used. The default is to convert the value of a string as if using the str() function. Refer to the table below for allowed values.

b Format an integer in binary.
c Given a number, display the character that has that code.
d Display a number in decimal (base 10).
e Display a float value using the exponential format.
E Same as e, but use a capital “E” in the exponent.
f Format a number in fixed-point form.
g General numeric format: use either f or g, whichever is appropriate.
G Same as “g”, but uses a capital “E” in the exponential form.
n For formatting numbers, this format uses the current local setting to insert separator characters. For example, a number that Americans would show as “1,234.56”, Europeans would show it as “1.234,56”.
o Display an integer in octal format.
x Display an integer in hexadecimal (base 16). Digits greater than 9 are displayed as lowercase characters.
X Display an integer in hexadecimal (base 16). Digits greater than 9 are displayed as uppercase characters.
% Display a number as a percentage: its value is multiplied by 100, followed by a “%” character.

Examples:

>>> "{:b}".format(9)
'1001'
>>> "{:08b}".format(9)
'00001001'
>>> "{:c}".format(97)
'a'
>>> "{:d}".format(0xff)
'255'
>>> from math import pi
>>> "{:e}".format(pi*1e10)
'3.141593e+10'
>>> "{:E}".format(pi*1e10)
'3.141593E+10'
>>> "{:f}".format(pi)
'3.141593'
>>> "{:g}".format(pi)
'3.14159'
>>> "{:g}".format(pi*1e37)
'3.14159e+37'
>>> "{:G}".format(pi*1e37)
'3.14159E+37'
>>> "{:o}".format(255)
'377'
>>> "{:#o}".format(255)
'0o377'
>>> "{:x}".format(105199)
'19aef'
>>> "{:X}".format(105199)
'19AEF'
>>> "{:<#9X}".format(105199)
'0X19AEF  '
>>> "{:%}".format(0.6789)
'67.890000%'
>>> "{:15.3%}".format(0.6789)
'        67.890%'