Skip to content

Instantly share code, notes, and snippets.

@gauthamchettiar
Last active April 10, 2024 10:47
Show Gist options
  • Save gauthamchettiar/fa3f32758616611bfecf941809fd598b to your computer and use it in GitHub Desktop.
Save gauthamchettiar/fa3f32758616611bfecf941809fd598b to your computer and use it in GitHub Desktop.
# Format Specification Mini-Language (Quick Reference)
import datetime
integer = 5
negative_integer = -5
decimal = 6.9
negative_decimal = -6.9
string = 'hi'
cost_float = 1299.99
cost_integer = 150000
date = datetime.datetime(2021, 7, 4, 12, 15, 58)
# ALLIGNING A VARIABLE/LITERAL
# round brackets () are not necessary,
# they are being used to understand where
# formatted string starts and ends
print(f'({integer:>6})') # ( 5)
print(f'({integer:<6})') # (5 )
print(f'({integer:^6})') # ( 5 )
print(f'({1:>6})') # ( 1)
print(f'({1:<6})') # (1 )
print(f'({1:^6})') # ( 1 )
print(f'({negative_integer:>6})') # ( -5)
print(f'({negative_integer:<6})') # (-5 )
print(f'({negative_integer:^6})') # ( -5 )
print(f'({decimal:>6})') # ( 6.9)
print(f'({negative_decimal:<6})') # (-6.9 )
print(f'({string:^6})') # ( hi )
# (ONLY VALID FOR NUMBER TYPES)
print(f'({negative_integer:=6})') # (- 5)
print(f'({integer:=6})') # ( 5)
print(f'({negative_decimal:=6})') # (- 6.9)
print(f'({decimal:=6})') # ( 6.9)
try:
print(f'({string:=6})')
except ValueError as value_error:
print(value_error)
# '=' alignment not allowed in string format specifier
# ALLIGNING A VARIABLE WITH FILL VALUE
print(f'({integer:0>6})') # (000005)
print(f'({decimal:0<6})') # (6.9000)
print(f'({string:*^6})') # (**hi**)
# HOW SIGN SHOULD BE DISPLAYED
# (ONLY VALID FOR NUMBER TYPES)
# + -> print both negative and positive signs
print(f'({negative_integer:+})') # (-5)
print(f'({integer:+})') # (+5)
print(f'({negative_decimal:+})') # (-6.9)
print(f'({decimal:+})') # (+6.9)
# - -> print only negative sign (default setting)
print(f'({negative_integer:-})') # (-5)
print(f'({integer:-})') # (5)
print(f'({negative_decimal:-})') # (-6.9)
print(f'({decimal:-})') # (6.9)
# <space> -> print - for negative
# print <space> for positive
print(f'({negative_integer: })') # (-5)
print(f'({integer: })') # ( 5)
print(f'({negative_decimal: })') # (-6.9)
print(f'({decimal: })') # ( 6.9)
# MIXING SIGN AND ALLIGNMENT SPECIFIERS
print(f'({negative_integer:=+6})') # (- 5)
print(f'({integer:=+6})') # (+ 5)
print(f'({integer:=-6})') # ( 5)
print(f'({integer:= 6})') # ( 5)
# GROUPING OPTION FOR INTEGERS AND FLOATS
print(f'({cost_float:,})') # (1,299.99)
print(f'({cost_float:_})') # (1_299.99)
print(f'({cost_integer:,})') # (150,000)
print(f'({cost_integer:_})') # (150,000)
# INTEGER RE-PRESENTATION
print(f'({cost_integer:b})') # BINARY # (100100100111110000)
print(f'({cost_integer:c})') # UNICODE # (𤧰)
print(f'({cost_integer:d})') # DECIMAL # (150000)
print(f'({cost_integer:o})') # OCTAL # (444760)
print(f'({cost_integer:x})') # HEX # (249f0)
print(f'({cost_integer:X})') # HEX (UPPERCASE LETTERS) # (249F0)
print(f'({cost_integer:n})') # DECIMAL (AS PER LOCALE) # (150000)
# FLOAT RE-PRESENTATION
print(f'({cost_float:e})') # SCIENTIFIC NOTATION # (1.299990e+03)
print(f'({cost_float:E})') # SCIENTIFIC NOTATION # (1.299990E+03)
print(f'({cost_float:f})') # FLOAT # (1299.990000)
print(f'({cost_integer:f})') # FLOAT # (150000.000000)
print(f'({decimal:%})') # PERCENTAGE # (690.000000%)
# FLOATING POINT PRECISION
print(f'({cost_integer/7:.2f})') # (21428.57)
print(f'({cost_integer/7:.10f})') # (21428.57143)
print(f'({decimal:.2%})') # (690.00%)
# DATE FORMATTING
print(f'({date:%Y-%m-%d})') # (2021-07-04)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment