Most comments in this notebook are taken from https://docs.python.org/3/library/string.html#format-specification-mini-language.
In [ ]:
import math
"{field_name:format_spec}".format(...)
format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type]
fill ::= <any character>
align ::= "<" | ">" | "=" | "^"
sign ::= "+" | "-" | " "
width ::= integer
precision ::= integer
type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
In [ ]:
"{:03}".format(1)
In [ ]:
"{:.<9}".format(3)
In [ ]:
"{:.<9}".format(11)
In [ ]:
"{:.>9}".format(3)
In [ ]:
"{:.>9}".format(11)
In [ ]:
"{:.=9}".format(3)
In [ ]:
"{:.=9}".format(11)
In [ ]:
"{:.^9}".format(3)
In [ ]:
"{:.^9}".format(11)
In [ ]:
"{:+}".format(3)
In [ ]:
"{:+}".format(-3)
In [ ]:
"{:-}".format(3)
In [ ]:
"{:-}".format(-3)
In [ ]:
"{: }".format(3)
In [ ]:
"{: }".format(-3)
In [ ]:
"{:3}".format(3)
In [ ]:
"{:3}".format(11)
In [ ]:
"{}".format(math.pi)
In [ ]:
"{:.2f}".format(math.pi)
In the following examples, 9 is the total space for the number (i.e. including dot and decimals) thus the following example has 4 characters for decimals, one character for the dot and 4 remaining characters for integers.
In [ ]:
"{:9.4f}".format(math.pi)
In [ ]:
"{:9.4f}".format(12.123456789)
The available integer presentation types are:
In [ ]:
"{:}".format(21)
In [ ]:
"{:b}".format(21)
In [ ]:
"{:#b}".format(21)
In [ ]:
"{:c}".format(21)
In [ ]:
"{:d}".format(21)
In [ ]:
"{:o}".format(21)
In [ ]:
"{:#o}".format(21)
In [ ]:
"{:x}".format(21)
In [ ]:
"{:X}".format(21)
In [ ]:
"{:#x}".format(21)
In [ ]:
"{:#X}".format(21)
In [ ]:
"{:n}".format(21)
The available presentation types for floating point and decimal values are:
In [ ]:
"{}".format(math.pi)
In [ ]:
"{:e}".format(math.pi)
In [ ]:
"{:E}".format(math.pi)
In [ ]:
"{:f}".format(math.pi)
In [ ]:
"{:F}".format(math.pi)
In [ ]:
"{:g}".format(math.pi)
In [ ]:
"{:G}".format(math.pi)
In [ ]:
"{:n}".format(math.pi)
In [ ]:
"{:%}".format(math.pi)
In [ ]:
numbers = [1000000, 100000, 10000, 1000, 100, 10, 1, 0.1, 0.01, 0.001, 0.0001, 0.00001]
for number in numbers:
print("{:f}".format(number), end="\t")
print("{:e}".format(number), end="\t")
print("{:g}".format(number))
In [ ]:
numbers = [1000, 100, 10, 1, 0.1, 0.01, 0.001, 0.0001, 0.00001]
for number in numbers:
print("{:.2f}".format(number), end="\t\t")
print("{:.2e}".format(number), end="\t")
print("{:.2g}".format(number))
In [ ]:
numbers = [1000, 100, 10, 1, 0.1, 0.01, 0.001, 0.0001, 0.00001]
for number in numbers:
print("{:g}".format(number), end="\t")
print("{:.3g}".format(number), end="\t")
print("{:.2g}".format(number), end="\t")
print("{:.1g}".format(number), end="\t")
print("{:.0g}".format(number))
In [ ]:
numbers = [1234000, 123400, 12340, 1234, 123.4, 12.34, 1.234, 0.1234, 0.01234, 0.001234, 0.0001234, 0.00001234]
for number in numbers:
print("{:<10g}".format(number), end="\t")
print("{:<10.6g}".format(number), end="\t")
print("{:<10.5g}".format(number), end="\t")
print("{:<10.4g}".format(number), end="\t")
print("{:<10.3g}".format(number), end="\t")
print("{:<10.2g}".format(number), end="\t")
print("{:<10.1g}".format(number))
In [ ]: