Python Print Format
Using F-String
format_spec
F-String starts with prefix f or F. Any variable to be printed is specified as following syntax.
Syntax:f"Regular string content {variable:format_spec} regular content"Observe
- prefix f (or F also works).
- Regular string content
- Formatting part in { } curly brackets. It can be placed anywhere in f-string. It contains variable whose value to be printed as per format_spec.
# Example, int formatted output
a1 = 255
print( f"F-String int printing: {a1}" ) # without formatting
print( f"F-String int printing: {a1:}" ) # format_spec empty
print( F"F-String int printing: {a1:10d}" ) # width 10, decimal
format_spec Syntax:
[ [fill] align] [sign] [#] [0] [width] [grouping_option] [.precision] [type]
# format_spec details
format_spec ::= [[fill]align][sign][#][0][width][grouping_option][.precision][type]
fill ::= any character
align ::= "<" | ">" | "=" | "^"
sign ::= "+" | "-" | " "
width ::= digit+
grouping_option ::= "_" | ","
precision ::= digit+
type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
fill
fill character is feasible only when align is specified. Limitation: curly bracket characters "{" or "}" are not allowed as fill character.
Fill and align happens within specified width.
align
align - Pls refer below table.
Align option | Prints with... |
---|---|
< | left alignment (default for most of object) |
> | right alignment (default for numbers) |
∧ | center alignment |
= | sign symbol takes first space instead of get attached to number. |
sign
sign - Pls refer below table.
Sign | Prints with... |
---|---|
- | sign for only negative. No space for positive. |
+ | sign for both negative and positive. |
(Space) | sign for negative. Keep space for positive |
#
# suggests to print alternate form for some data type. Example, print binary with 0b prefix (regular form: binary without 0b).
0
0 is short form for fill character "0" and align "=".
width
width is decimal number including prefix, separators, formatting characters.
grouping_option
grouping_option suggests to use thousands separator "," for int (decimal) and float. "_" is also thousands separator for int (decimal) and float. Rather, "_" is more helpful with int binary, octal or hex, it groups 4 digits (for readability).
precision
precision indicates number of digits after decimal point for float when type is "f". OR total number of digits before and after decimal points for float when type is "g".
type
type indicates how variable is printed.
Pls refer below table for int type.
Type | Prints int as |
---|---|
b | binary |
c | unicode character |
d | decimal (default for int) |
o | Octal |
x | hex with lower case a-f |
X | hex with upper case A-F |
n | decimal with number separator (as per current locale) |
not specified | use default (i.e. d) |
Int formatting
print int - simple way
# Example, print int - simple way
a1 = 255
print( f"With some message, value of a1: {a1}" )
print int as bin
Pls refer int type format_spec.
# Example, print int as bin
a1 = 255
print( f"Int as bin : {a1:b}" ) # '11111111'
print( f"Int as bin : {a1:#b}" ) # '0b11111111'
print int as oct
Pls refer int type format_spec.
# Example, print int as oct
a1 = 255
print( f"Int as oct : {a1:o}" ) # '377'
print( f"Int as oct : {a1:#o}" ) # '0o377'
print int as hex
Pls refer int type format_spec.
# Example, print int as hex
a1 = 255
print( f"Int as hex : {a1:x}" ) # 'ff'
print( f"Int as hex : {a1:#x}" ) # '0xff'
print int with width
Pls refer width format_spec.
# Example, print int with 10 space width
a1 = 255
a2 = -255
print( f"Int with width : |{a1:10}|" ) # ' 255'
print( f"Int with width : |{a1:10d}|" ) # ' 255'
print( f"Int with width : |{a2:10d}|" ) # ' -255'
# Example, print large int with lower width
a1 = 2**30
print( f"Int with smaller width: |{a1:4d}|" ) # ignores width option
print int with leading 0
Pls refer 0 padding format_spec. To fill character other than 0, pls refer Padding other than 0.
# Example, print int with leading 0
a1 = 255
print( f"Int with leading 0 : |{a1:010d}|" ) # '0000000255'
print int with sign
Pls refer sign format_spec.
# Example, print int with sign
a1 = 255
a2 = -255
print( f"Int only negative sign : |{a1:-d}|" ) # '255'
print( f"Int only negative sign : |{a2:-d}|" ) # '-255'
print( f"Int both signs : |{a1:+d}|" ) # '+255'
print( f"Int both signs : |{a2:+d}|" ) # '-255'
print( f"Int negative sign or space : |{a1: d}|" ) # ' 255'
print( f"Int negative sign or space : |{a2: d}|" ) # '-255'
print int with alignment
Pls refer align format_spec.
# Example, print int with sign
a1 = 255
a2 = -255
print( f"Int left align : |{a1:<10d}|" ) # '255 '
print( f"Int left align : |{a2:<10d}|" ) # '-255 '
print( f"Int right align : |{a1:>10d}|" ) # ' 255'
print( f"Int right align : |{a2:>10d}|" ) # ' -255'
print( f"Int center align : |{a1:^10d}|" ) # ' 255 '
print( f"Int center align : |{a2:^10d}|" ) # ' -255 '
print( f"Int sign as first: |{a1:=10d}|" ) # ' 255'
print( f"Int sign as first: |{a2:=10d}|" ) # '- 255'
print( f"Int sign attached: |{a1:10d}|" ) # ' 255'
print( f"Int sign attached: |{a2:10d}|" ) # ' -255'
print int with padding other than 0
Pls refer fill format_spec.
# Example, print int with sign
a1 = 255
a2 = -255
print( f"Int fill : |{a1:*<10d}|" ) # '255*******'
print( f"Int fill : |{a2:*<10d}|" ) # '-255******'
print( f"Int fill : |{a1:?>10d}|" ) # '???????255'
print( f"Int fill : |{a2:?>10d}|" ) # '??????-255'
print( f"Int fill : |{a1:_^10d}|" ) # '___255____'
print( f"Int fill : |{a2:_^10d}|" ) # '___-255___'
print( f"Int fill : |{a1:#=10d}|" ) # '#######255'
print( f"Int fill : |{a2:#=10d}|" ) # '-######255'
print int with grouping_option
Pls refer grouping_option format_spec.
# Example, print int with grouping_option
a1 = 10**7
a2 = -10**7
print( f"Int grouping : |{a1:15,d}|" ) # ' 10,000,000'
print( f"Int grouping : |{a2:15_d}|" ) # ' -10_000_000'
print bin, oct, hex with grouping_option
Pls refer grouping_option format_spec.
# Example, print bin, oct, hex with grouping_option
a2 = -2**13
print( f"bin grouping : |{a2:#20,b}|" ) # raises ValueError
print( f"bin grouping : |{a2:#20_b}|" ) # '-0b10_0000_0000_0000'
print( f"oct grouping : |{a2:#20_o}|" ) # ' -0o2_0000'
print( f"hex grouping : |{a2:#20_X}|" ) # ' -0X2000'
ValueError: Cannot specify ',' with 'b'
Why this error: grouping_option ',' does not work with binary / octal / hex. Solution: Use '_' grouping_option for binary / octal / hex.Similar cases:
ValueError: Cannot specify ',' with 'o'.
ValueError: Cannot specify ',' with 'x'.
# Example, print bin, oct, hex with grouping_option
a1 = 2**10
a2 = -2**10
print( f"bin : |{a1:#20,b}|" ) # raises ValueError
print( f"bin : |{a2:#20_b}|" ) # ' -0b100_0000_0000'
print int with fill, align, sign, width, grouping_option, type
Pls refer fill, align, sign, width, grouping_option, int type format_spec.
Below example is given for one sample case.
fill - *
align - left
sign - both
width - 10
grouping_option - ,
type - int decimal
# Example, print int with fill, align, sign, width, grouping_option, type
a1 = 10**4
a2 = -10**4
print( f"Int : |{a1:*<+10,d}|" ) # '+10,000***'
print( f"Int : |{a2:*<+10,d}|" ) # '-10,000***'