This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)
*Memos:
:[f][a][s][z][#][0][w][g][.p][t] can format a string as shown below. *Format Specification Mini-Language explains more details:
*Memos
-
:must be used withf,a,s,z,#,0,w,g,.port. -
f(fill) is the zero or one character to fill the left and/or right side of the padding of a string. -
a(align) is to align a string with^,<,>or=: *Memos:-
^can center a string. -
<can left-align(left-justify) a string. -
>can right-align(right-justify) a string. -
=can right-align(right-justify) a string only withintorfloatinput. *Padding is added between+or-and input.
-
-
s(sign) is+,-, or ” ” which can be used only withint,floatorcomplexinput: *Memos:-
+indicates that a sign should be used for both positive and negative numbers. -
-indicates that a sign should be used only for negative numbers, which is the default ifsisn’t set. -
" "indicates that a leading space should be used on positive numbers, and a minus sign on negative numbers.
-
-
zis to make a negative zero a positive zero. *zcan be used only withfloatorcomplexinput. -
#is to convert a string to other form. *#can be used only withint,floatorcomplexinput. -
0is to fill the left and/or right side of the one or more available spaces of a string with0. It’s equivalent to thefcharacter0. -
wis to decide the width of a string: *Memos:- It must be
0 <= integerif it’s set. - If it’s not set, alignment doesn’t occur.
- It must be
-
g(grouping) is to separate digits with,or_. *gcan be used only withint,floatorcomplexinput. -
.p(.precision) is to decide the scale or precision of a number or the number of the characters of a string: *Memos:- For
forF, scale(the number of the digits after the decimal point) is decided. *Scale is displayed even if they are zero. - For
gorG, precision(the total number of the digits before and after the decimal point is decided as much as possible. *The digits after the decimal point aren’t displayed if they are zero. - For
s, the number of characters is decided.
- For
-
t(type) is to decide how data must be presented withsforstrinput,b,c,d,o,x,Xornforintinput ore,E,f,F,g,G,nor%forfloat,complexand Decimal() input: *Memos:-
sis string format. *Iftisn’t set forstrinput,tis default tos. -
dis decimal integer. *Iftisn’t set forintinput,tis default tod. -
fis fixed-point notation. *If.pis set, scale is.pdigits while if.pisn’t set, scale is 6 digits and if.0is set, scale is omitted unless#is set. -
Fis same asfexcept that uppercaseNANandINFare used. -
gis general format. -
Gis same asgexcept that uppercaseE,INFINITYandNANare used. - If
tisn’t set forfloatorcomplex,g-like operation is done. - If
tisn’t set forDecimal(),gorGis set, depending on the value ofcontext.capitals. -
The doc explains other types like
b,c,d, etc.
-
- If both
fand0are set,fis prioritized. - If both
fand0aren’t set, the default is" ". -
f=0and/or0cannot be used withcomplexinput.
<Put everything all together>:
v = -0.0
print('"{:0>-z#010_.0f}"'.format(v))
print('"{:0>-z#10_.0f}"'.format(v))
print('"{:>-z#010_.0f}"'.format(v))
# "000000000."
print('"{0:0>-z#010_.0f}"'.format(v))
print('"{0:0>-z#10_.0f}"'.format(v))
print('"{0:>-z#010_.0f}"'.format(v))
# "000000000."
print('"{:0>-z#010_.0f}" "{:0>-z#10_.0f}" "{:>-z#010_.0f}"'.format(v, v, v))
print('"{0:0>-z#010_.0f}" "{1:0>-z#10_.0f}" "{2:>-z#010_.0f}"'.format(v, v, v))
# "000000000." "000000000." "000000000."
<Center a string with ‘^’>:
*It’s like center().
v = "hello world"
print('"{:^20}"'.format(v))
print('"{: ^20}"'.format(v))
# " hello world "
# ↑↑↑↑ ↑↑↑↑↑
print('"{:?^20}"'.format(v))
# "????hello world?????"
print('"{:?^17}"'.format(v))
# "???hello world???"
print('"{:?^14}"'.format(v))
# "?hello world??"
print('"{:?^13}"'.format(v))
# "?hello world?"
print('"{:?^12}"'.format(v))
# "hello world?"
print('"{:?^11}"'.format(v))
print('"{:?^0}"'.format(v))
print('"{:?^}"'.format(v))
print('"{:^}"'.format(v))
print('"{:}"'.format(v))
print('"{}"'.format(v))
# "hello world"
v = "hello world "
# ↑↑↑
print('"{:?^14}"'.format(v))
print('"{:?^0}"'.format(v))
# "hello world "
# ↑↑↑
print('"{:?^20}"'.format(v))
# "???hello world ???"
# ↑↑↑
v = " hello world"
# ↑↑↑
print('"{:?^14}"'.format(v))
print('"{:?^0}"'.format(v))
# " hello world"
# ↑↑↑
print('"{:?^20}"'.format(v))
# "??? hello world???"
# ↑↑↑
<Left-align a string with ‘<‘>:
*It’s like ljust().
v = "hello world"
print('"{:<20}"'.format(v))
print('"{: <20}"'.format(v))
# "hello world "
# ↑↑↑↑↑↑↑↑↑
print('"{:?<20}"'.format(v))
# "hello world?????????"
print('"{:?<17}"'.format(v))
# "hello world??????"
print('"{:?<14}"'.format(v))
# "hello world???"
print('"{:?<13}"'.format(v))
# "hello world??"
print('"{:?<12}"'.format(v))
# "hello world?"
print('"{:?<11}"'.format(v))
print('"{:?<0}"'.format(v))
print('"{:?<}"'.format(v))
print('"{:<}"'.format(v))
print('"{:}"'.format(v))
print('"{}"'.format(v))
# "hello world"
v = " hello world"
# ↑↑↑
print('"{:?<14}"'.format(v))
print('"{:?<0}"'.format(v))
# " hello world"
# ↑↑↑
print('"{:?<20}"'.format(v))
# " hello world??????"
# ↑↑↑
v = " hello world "
# ↑↑↑ ↑↑↑
print('"{:?<14}"'.format(v))
print('"{:?<0}"'.format(v))
# " hello world "
# ↑↑↑ ↑↑↑
print('"{:?<20}"'.format(v))
# " hello world ???"
# ↑↑↑ ↑↑↑
This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)
