Format


In [ ]:
str('0'), repr('0'), format('0'), '{}'.format('0'), '{!s}'.format('0'), '{!r}'.format('0')

In [ ]:
print('|{}|{}|{}|'.format('ABC', 1, 1.))
print('|{2}|{1}|{0}|{1}|{2}|'.format('ABC', 1, 1.))
print('|{v2}|{0}|{v1}|'.format('ABC', v1 = 1, v2 = 1.))
print('|{1[v11]}|{1[v10]}|{0}|'.format('ABC', {'v10': 1, 'v11': 1.}))
print('|{v1[v11]}|{v1[v10]}|{0[0]}|'.format('ABC', v1 = {'v10': 1, 'v11': 1.}))
print('|{1[1]}|{1[0]}|{0[1]}|'.format('ABC', [1,  1.]))
print('|{v1[1]}|{v1[0]}|{0[2]}|'.format('ABC', v1 = [1,  1.]))

The general form of a standard format specifier is

                [[fill]align][sign][#][0][width][,][.precision][type]

where:

  • fill is any character,
  • align is "<", ">", "=" or "^"
  • sign is "+", "-" or " "
  • width and precision are integers
  • type is "b", "c", "d", "e", "E", "f", "F", "g", "G", "n", "o", "s", "x", "X" or "%"

In [ ]:
# Can be replaced by:
# print('|{:9s}|{:<9s}|{:>9s}|{:^9s}|'.format('ABCDE', 'ABCDE', 'ABCDE', 'ABCDE', 'ABCDE'))
print('|{:9}|{:<9}|{:>9}|{:^9}|'.format('ABCDE', 'ABCDE', 'ABCDE', 'ABCDE'))

# Can be replaced by:
# print('|{:9s}|{:_<9s}|{:_>9s}|{:_^9s}|'.format('', 'ABCDE', 'ABCDE', 'ABCDE', 'ABCDE'))
print('|{:9}|{:_<9}|{:_>9}|{:_^9}|'.format('', 'ABCDE', 'ABCDE', 'ABCDE'))

# Can be replaced by:
# print('|{:.5s}|'.format('ABCDEFGHIJ'))
print('|{:.5}|'.format('ABCDEFGHIJ'))

In [ ]:
print('|{:c}|'.format(9731))
print('|{:9c}|{:<9c}|{:>9c}|{:^9c}|'.format(9731, 9731, 9731, 9731))
print('|{:09c}|{:_<9c}|{:_>9c}|{:_^9c}|'.format(9731, 9731, 9731, 9731))

In [ ]:
# Can be replaced by either:
# print('|{:8d}|{:<8d}|{:>8d}|{:=8d}|{:^8d}|'.format(-123, -123, -123, -123, -123))
# or:
# print('|{:8n}|{:<8n}|{:>8n}|{:=8n}|{:^8n}|'.format(-123, -123, -123, -123, -123))
print('|{:8}|{:<8}|{:>8}|{:=8}|{:^8}|'.format(-123, -123, -123, -123, -123))

# Can be replaced by either:
# print('|{:08d}|{:_<8d}|{:_>8d}|{:_=8d}|{:_^8d}|'.format(-123, -123, -123, -123, -123))
# or:
# print('|{:08n}|{:_<8n}|{:_>8n}|{:_=8n}|{:_^8n}|'.format(-123, -123, -123, -123, -123))
print('|{:08}|{:_<8}|{:_>8}|{:_=8}|{:_^8}|'.format(-123, -123, -123, -123, -123))

# Can be replaced by:
# print('{:d}|{:+d}|{:-d}|{: d}|'.format(+123, 123, 123, 123))
# or:
# print('{:n}|{:+n}|{:-n}|{: n}|'.format(+123, 123, 123, 123))
print('|{}|{:+}|{:-}|{: }|'.format(+123, 123, 123, 123))

# Can be replaced by either:
# print('|{:,d}|'.format(-12345678))
# or:
# print('|{:,n}|'.format(-12345678))
print('|{:,}|'.format(-12345678))

In [ ]:
print('|{:12b}|{:<12b}|{:>12b}|{:=12b}|{:^12b}|'.format(-37, -37, -37, -37, -37))
print('|{:012b}|{:_<12b}|{:_>12b}|{:_=12b}|{:_^12b}|'.format(-37, -37, -37, -37, -37))
print('|{:b}|{:+b}|{:-b}|{: b}|'.format(+37, 37, 37, 37))
print()

print('|{:#12b}|{:<#12b}|{:>#12b}|{:=#12b}|{:^#12b}|'.format(-37, -37, -37, -37, -37))
print('|{:#012b}|{:_<#12b}|{:_>#12b}|{:_=#12b}|{:_^#12b}|'.format(-37, -37, -37, -37, -37))
print('|{:#b}|{:+#b}|{:-#b}|{: #b}|'.format(37, 37, 37, 37))

In [ ]:
print('|{:9o}|{:<9o}|{:>9o}|{:=9o}|{:^9o}|'.format(-69, -69, -69, -69, -69))
print('|{:09o}|{:_<9o}|{:_>9o}|{:_=9o}|{:_^9o}|'.format(-69, -69, -69, -69, -69))
print('|{:o}|{:+o}|{:-o}|{: o}|'.format(+69, 69, 69, 69))
print()

print('|{:#9o}|{:<#9o}|{:>#9o}|{:=#9o}|{:^#9o}|'.format(-69, -69, -69, -69, -69))
print('|{:#09o}|{:_<#9o}|{:_>#9o}|{:_=#9o}|{:_^#9o}|'.format(-69, -69, -69, -69, -69))
print('|{:#o}|{:+#o}|{:-#o}|{: #o}|'.format(69, 69, 69, 69))

In [ ]:
print('|{:9x}|{:<9x}|{:>9x}|{:=9x}|{:^9x}|'.format(-27, -27, -27, -27, -27))
print('|{:09x}|{:_<9x}|{:_>9x}|{:_=9x}|{:_^9x}|'.format(-27, -27, -27, -27, -27))
print('|{:x}|{:+x}|{:-x}|{: x}|'.format(+27, 27, 27, 27))
print()

print('|{:#9x}|{:<#9x}|{:>#9x}|{:=#9x}|{:^#9x}|'.format(-27, -27, -27, -27, -27))
print('|{:#09x}|{:_<#9x}|{:_>#9x}|{:_=#9x}|{:_^#9x}|'.format(-27, -27, -27, -27, -27))
print('|{:#x}|{:+#x}|{:-#x}|{: #x}|'.format(27, 27, 27, 27))

In [ ]:
print('|{:9X}|{:<9X}|{:>9X}|{:=9X}|{:^9X}|'.format(-27, -27, -27, -27, -27))
print('|{:09X}|{:_<9X}|{:_>9X}|{:_=9X}|{:_^9X}|'.format(-27, -27, -27, -27, -27))
print('|{:X}|{:+X}|{:-X}|{: X}|'.format(+27, 27, 27, 27))
print()

print('|{:#9X}|{:<#9X}|{:>#9X}|{:=#9X}|{:^#9X}|'.format(-27, -27, -27, -27, -27))
print('|{:#09X}|{:_<#9X}|{:_>#9X}|{:_=#9X}|{:_^#9X}|'.format(-27, -27, -27, -27, -27))
print('|{:#X}|{:+#X}|{:-#X}|{: #X}|'.format(27, 27, 27, 27))

In [ ]:
print('|{:10}|{:<25}|{:>25}|{:=25}|{:^25}|'.format(
        -1234., -123456789012345.6789, -1234567890123456.789,
        -12345678901234567.89, -123456789012345678.9))
print('|{:010}|{:_<25}|{:_>25}|{:_=25}|{:_^25}|'.format(
        -1234., -123456789012345.6789, -1234567890123456.789,
        -12345678901234567.89, -123456789012345678.9))
print()

print('|{}|{:+}|{:-}|{: }|'.format(+123456789012345.6789, 1234567890123456.789,
                                   12345678901234567.89, 123456789012345678.9))
print('|{:,}|'.format(-12345.6789))
print('|{:.5}|{:.5}|'.format(-12.3456789, -123456789012345678.9))
print('|{:.0}|{:#.0}|'.format(12.34, 12.34))
print()

print('{:} {:}'.format(float('inf'), float('Nan')))

In [ ]:
# Can be replaced by:
# print('|{:15F}|{:<15F}|{:>15F}|{:=15F}|{:^15F}|'.format(
#         -12.3456789, -12.3456789, -12.3456789, -12.3456789, -12.3456789))
print('|{:15f}|{:<15f}|{:>15f}|{:=15f}|{:^15f}|'.format(
        -12.3456789, -12.3456789, -12.3456789, -12.3456789, -12.3456789))
# Can be replaced by:
# print('|{:015F}|{:_<15F}|{:_>15F}|{:_=15F}|{:_^15F}|'.format(
#         -12.3456789, -12.3456789, -12.3456789, -12.3456789, -12.3456789))
print('|{:015f}|{:_<15f}|{:_>15f}|{:_=15f}|{:_^15f}|'.format(
        -12.3456789, -12.3456789, -12.3456789, -12.3456789, -12.3456789))
print()

# Can be replaced by:
# print('|{:F}|{:+F}|{:-F}|{: F}|'.format(+123456789., 123456789., 123456789., 123456789.))
print('|{:f}|{:+f}|{:-f}|{: f}|'.format(+123456789., 123456789., 123456789., 123456789.))
# Can be replaced by:
# print('|{:,F}|'.format(-12345.6789))
print('|{:,f}|'.format(-12345.6789))
# Can be replaced by:
# print('|{:.5F}|'.format(-12.3456789))
print('|{:.5f}|'.format(-12.3456789))
# Can be replaced by:
# print('|{:.0F}|{:#.0F}|'.format(12.34, 12.34))
print('|{:.0f}|{:#.0f}|'.format(12.34, 12.34))
print()

print('{:f} {:f}'.format(float('inf'), float('Nan')))
print('{:F} {:F}'.format(float('inf'), float('Nan')))

In [ ]:
print('|{:15e}|{:<15e}|{:>15e}|{:=15e}|{:^15e}|'.format(
        -12.3456789, -12.3456789, -12.3456789, -12.3456789, -12.3456789))
print('|{:15E}|{:<15E}|{:>15E}|{:=15E}|{:^15E}|'.format(
        -12.3456789, -12.3456789, -12.3456789, -12.3456789, -12.3456789))
print()

print('|{:015e}|{:_<15e}|{:_>15e}|{:_=15e}|{:_^15e}|'.format(
        -12.3456789, -12.3456789, -12.3456789, -12.3456789, -12.3456789))
print('|{:015E}|{:_<15E}|{:_>15E}|{:_=15E}|{:_^15E}|'.format(
        -12.3456789, -12.3456789, -12.3456789, -12.3456789, -12.3456789))
print()

print('|{:e}|{:+e}|{:-e}|{: e}|'.format(+12.3456789, 12.3456789, 12.3456789, 12.3456789))
print('|{:E}|{:+E}|{:-E}|{: E}|'.format(+12.3456789, 12.3456789, 12.3456789, 12.3456789))
print()

print('|{:.5e}|'.format(-12.3456789))
print('|{:.5E}|'.format(-12.3456789))
print()

print('|{:.0e}|{:#.0e}|'.format(12.34, 12.34))
print('|{:.0E}|{:#.0E}|'.format(12.34, 12.34))
print()

print('{:e} {:e}'.format(float('inf'), float('Nan')))
print('{:E} {:E}'.format(float('inf'), float('Nan')))

In [ ]:
print('|{:15g}|{:<15g}|{:>15g}|{:=15g}|{:^15g}|'.format(
        -1234., -12345.6789, -123456.789, -1234567.89, -12345678.9))
print('|{:15G}|{:<15G}|{:>15G}|{:=15G}|{:^15G}|'.format(
        -1234., -12345.6789, -123456.789, -1234567.89, -12345678.9))
print()

print('|{:015g}|{:_<15g}|{:_>15g}|{:_=15g}|{:_^15g}|'.format(
        -1234.56789, -12345.6789, -123456.789, -1234567.89, -12345678.9))
print('|{:015G}|{:_<15G}|{:_>15G}|{:_=15G}|{:_^15G}|'.format(
        -1234.56789, -12345.6789, -123456.789, -1234567.89, -12345678.9))
print()

print('|{:g}|{:+g}|{:-g}|{: g}|'.format(+1234.56789, 12345.6789, 123456.789, 1234567.89))
print('|{:G}|{:+G}|{:-G}|{: G}|'.format(+1234.56789, 12345.6789, 123456.789, 1234567.89))
print()

print('|{:.5g}|{:.5g}|'.format(-12.3456789, -1234567.89))
print('|{:.5G}|{:.5G}|'.format(-1234567.89, -1234567.89))
print()

print('|{:g}|{:#g}|{:g}|{:#g}|'.format(12., 12., 10000000, 10000000))
print('|{:G}|{:#G}|{:G}|{:#G}|'.format(12., 12., 10000000, 10000000))
print()

print('{:g} {:g}'.format(float('inf'), float('Nan')))
print('{:G} {:G}'.format(float('inf'), float('Nan')))

In [ ]:
print('|{:15%}|{:<15%}|{:>15%}|{:=15%}|{:^15%}|'.format(
        -12.3456789, -12.3456789, -12.3456789, -12.3456789, -12.3456789))
print('|{:015%}|{:_<15%}|{:_>15%}|{:_=15%}|{:_^15%}|'.format(
        -12.3456789, -12.3456789, -12.3456789, -12.3456789, -12.3456789))
print()

print('|{:%}|{:+%}|{:-%}|{: %}|'.format(+12.345, 12.345, 12.345, 12.345))
print('|{:,%}|'.format(-12345.6789))
print('|{:.5%}|'.format(-12.3456789))