*Memo:
- My post explains format specification with format() (2).
- My post explains format specification with format() (3).
- My post explains format specification with format() (4).
- My post explains format specification with format() (5).
- My post explains format().
:[f][a][s][z][#][0][w][g][.p][t] can format a string with format() as shown below:
*Memo:
- Format Specification Mini-Language explains more.
-
: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 the string setw. -
a(align) is to align(justify) a string with^,<,>or=:-
^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.
- Padding is added between
-
-
s(sign) is+,-or " " which can be used only withint,floatorcomplexinput:-
+indicates that a sign should be used with both positive and negative numbers. -
-indicates that a sign should be used only with negative numbers, which is the default ifsisn't set. -
" "indicates that a leading space should be used with positive numbers and a minus sign should be used with negative numbers.
-
-
zconverts a negative zero to a positive zero:- It can be used only with
floatorcomplexinput.
- It can be used only with
-
#converts a string to other form:- It can be used only with
int,floatorcomplexinput.
- It can be used only with
-
0fills the left and/or right side of the padding of the string setwwith0:- It's equivalent to the
fcharacter0.
- It's equivalent to the
-
w(width) is the width of a string:- Padding is added if
wis longer than the length of the original string:- For
^, padding starts to be added from the right side of a string ifwis oddly longer than the length of the original string.
- For
- It must be
0 <= intif it's set. - If it's not set, alignment doesn't occur.
- Padding is added if
-
g(grouping) separates digits with,or_:- It can be used only with
int,floatorcomplexinput. -
Decimal() can be used only with
,.
- It can be used only with
-
.p(.precision) is the scale or precision of a number or the number of the characters of a string:-
Scale means the number of the digits after the decimal point:
- Scale is also called decimal part or fractional part.
- Precision means the total number of the digits before and after the decimal point.
- For
forFoft(type), scale is decided:- Scale is displayed even if they are zero.
- For
gorGoft(type), precision 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.
-
Scale means the number of the digits after the decimal point:
-
t(type) decides how data must be presented withsforstrinput,b,c,d,o,x,Xornforintinput ore,E,f,F,g,G,nor%forint,float,complexorDecimal()input:-
sis string format:- If
tisn't set forstrinput,tis default tos.
- If
-
dis decimal integer:- If
tisn't set forintinput,tis default tod.
- If
-
fis fixed-point notation:- If
.pis set, scale is.pdigits. - If
.pisn't set, scale is 6 digits forint,floatorcomplexand scale is the original digits forDecimal(). - If
.0is set, scale is omitted unless#is set. - Unnecessary zeros in scale aren't removed.
- If
.pis less than scale, the value is rounded.
- If
-
Fis same asfexcept that uppercaseNANandINFare used. -
gis general format:- If
.pis set, precision is.pdigits. - If
.pisn't set, precision is 6 digits forint,floatorcomplexand precision is the original digits forDecimal(). -
.0is treated as.1. - Unnecessary zeros in scale are removed.
- If
.pis less than precision, the value is rounded. - If
.pis less than integer part(the number of the digits before the decimal point), a scientific notation is used.
- If
-
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. -
Decimal()should be used withstrto get a proper value. -
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 '^'>:
*Memo:
- It's like center().
v = "John Smith"
print('"{:^20}"'.format(v))
print('"{: ^20}"'.format(v))
# " John Smith "
# ↑↑↑↑↑ ↑↑↑↑↑
print('"{:?^20}"'.format(v))
# "?????John Smith?????"
print('"{:?^17}"'.format(v))
# "???John Smith????"
print('"{:?^14}"'.format(v))
# "??John Smith??"
print('"{:?^13}"'.format(v))
# "?John Smith??"
print('"{:?^12}"'.format(v))
# "?John Smith?"
print('"{:?^11}"'.format(v))
# "John Smith?"
print('"{:?^10}"'.format(v))
print('"{:?^0}"'.format(v))
print('"{:?^}"'.format(v))
print('"{:^}"'.format(v))
print('"{:}"'.format(v))
print('"{}"'.format(v))
# "John Smith"
v = " John Smith "
# ↑↑↑ ↑↑↑
print('"{:?^0}"'.format(v))
# " John Smith "
# ↑↑↑ ↑↑↑
print('"{:?^20}"'.format(v))
# "?? John Smith ??"
# ↑↑↑ ↑↑↑
<Left-align a string with '<'>:
*Memo:
- It's like ljust().
v = "John Smith"
print('"{:<20}"'.format(v))
print('"{: <20}"'.format(v))
# "John Smith "
# ↑↑↑↑↑↑↑↑↑↑
print('"{:?<20}"'.format(v))
# "John Smith??????????"
print('"{:?<17}"'.format(v))
# "John Smith???????"
print('"{:?<14}"'.format(v))
# "John Smith????"
print('"{:?<13}"'.format(v))
# "John Smith???"
print('"{:?<12}"'.format(v))
# "John Smith??"
print('"{:?<11}"'.format(v))
# "John Smith?"
print('"{:?<10}"'.format(v))
print('"{:?<0}"'.format(v))
print('"{:?<}"'.format(v))
print('"{:<}"'.format(v))
print('"{:}"'.format(v))
print('"{}"'.format(v))
# "John Smith"
v = " John Smith "
# ↑↑↑ ↑↑↑
print('"{:?<0}"'.format(v))
# " John Smith "
# ↑↑↑ ↑↑↑
print('"{:?<20}"'.format(v))
# " John Smith ????"
# ↑↑↑ ↑↑↑
Top comments (0)