*Memos:
- 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 f-strings.
- My post explains format().
- My post explains format_map().
- My post explains a string.
:[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
,.p
ort
. -
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 withint
orfloat
input:- Padding is added between
+
or-
and input.
- Padding is added between
-
-
s
(sign
) is+
,-
or " " which can be used only withint
,float
orcomplex
input:-
+
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 ifs
isn't set. -
" "
indicates that a leading space should be used with positive numbers and a minus sign should be used with negative numbers.
-
-
z
converts a negative zero to a positive zero:- It can be used only with
float
orcomplex
input.
- It can be used only with
-
#
converts a string to other form:- It can be used only with
int
,float
orcomplex
input.
- It can be used only with
-
0
fills the left and/or right side of the padding of the string setw
with0
:- It's equivalent to the
f
character0
.
- It's equivalent to the
-
w
(width
) is the width of a string:- Padding is added if
w
is longer than the length of the original string:- For
^
, padding starts to be added from the right side of a string ifw
is oddly longer than the length of the original string.
- For
- It must be
0 <= int
if 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
,float
orcomplex
input. -
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
f
orF
oft
(type
), scale is decided:- Scale is displayed even if they are zero.
- For
g
orG
oft
(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 withs
forstr
input,b
,c
,d
,o
,x
,X
orn
forint
input ore
,E
,f
,F
,g
,G
,n
or%
forint
,float
,complex
orDecimal()
input:-
s
is string format:- If
t
isn't set forstr
input,t
is default tos
.
- If
-
d
is decimal integer:- If
t
isn't set forint
input,t
is default tod
.
- If
-
f
is fixed-point notation:- If
.p
is set, scale is.p
digits. - If
.p
isn't set, scale is 6 digits forint
,float
orcomplex
and scale is the original digits forDecimal()
. - If
.0
is set, scale is omitted unless#
is set. - Unnecessary zeros in scale aren't removed.
- If
.p
is less than scale, the value is rounded.
- If
-
F
is same asf
except that uppercaseNAN
andINF
are used. -
g
is general format:- If
.p
is set, precision is.p
digits. - If
.p
isn't set, precision is 6 digits forint
,float
orcomplex
and precision is the original digits forDecimal()
. -
.0
is treated as.1
. - Unnecessary zeros in scale are removed.
- If
.p
is less than precision, the value is rounded. - If
.p
is less than integer part(the number of the digits before the decimal point), a scientific notation is used.
- If
-
G
is same asg
except that uppercaseE
,INFINITY
andNAN
are used. - If
t
isn't set forfloat
orcomplex
,g
-like operation is done. - If
t
isn't set forDecimal()
,g
orG
is set, depending on the value ofcontext.capitals
. -
Decimal()
should be used withstr
to get a proper value. -
The doc explains other types like
b
,c
,d
, etc.
-
- If both
f
and0
are set,f
is prioritized. - If both
f
and0
aren't set, the default is" "
. -
f=0
and/or0
cannot be used withcomplex
input.
<Put everything all together>:
v = -0.0
print(f'"{v:0>-z#010_.0f}"')
print(f'"{v:0>-z#10_.0f}"')
print(f'"{v:>-z#010_.0f}"')
# "000000000."
print(f'"{v:0>-z#010_.0f}" "{v:0>-z#10_.0f}" "{v:>-z#010_.0f}"')
print(f'"{v:0>-z#010_.0f}" "{v:0>-z#10_.0f}" "{v:>-z#010_.0f}"')
# "000000000." "000000000." "000000000."
<Center a string with '^'>:
*It's like center().
v = "John Smith"
print(f'"{v:^20}"')
print(f'"{v: ^20}"')
# " John Smith "
# ↑↑↑↑↑ ↑↑↑↑↑
print(f'"{v:?^20}"')
# "?????John Smith?????"
print(f'"{v:?^17}"')
# "???John Smith????"
print(f'"{v:?^14}"')
# "??John Smith??"
print(f'"{v:?^13}"')
# "?John Smith??"
print(f'"{v:?^12}"')
# "?John Smith?"
print(f'"{v:?^11}"')
# "John Smith?"
print(f'"{v:?^0}"')
print(f'"{v:?^}"')
print(f'"{v:^}"')
print(f'"{v:}"')
print(f'"{v}"')
# "John Smith"
v = " John Smith "
# ↑↑↑ ↑↑↑
print(f'"{v:?^0}"')
# " John Smith "
# ↑↑↑ ↑↑↑
print(f'"{v:?^20}"')
# "?? John Smith ??"
# ↑↑↑ ↑↑↑
<Left-align a string with '<'>:
*It's like ljust().
v = "John Smith"
print(f'"{v:<20}"')
print(f'"{v: <20}"')
# "John Smith "
# ↑↑↑↑↑↑↑↑↑↑
print(f'"{v:?<20}"')
# "John Smith??????????"
print(f'"{v:?<17}"')
# "John Smith???????"
print(f'"{v:?<14}"')
# "John Smith????"
print(f'"{v:?<13}"')
# "John Smith???"
print(f'"{v:?<12}"')
# "John Smith??"
print(f'"{v:?<11}"')
# "John Smith?"
print(f'"{v:?<0}"')
print(f'"{v:?<}"')
print(f'"{v:<}"')
print(f'"{v:}"')
print(f'"{v}"')
# "John Smith"
v = " John Smith "
# ↑↑↑ ↑↑↑
print(f'"{v:?<0}"')
# " John Smith "
# ↑↑↑ ↑↑↑
print(f'"{v:?<20}"')
# " John Smith ????"
# ↑↑↑ ↑↑↑
Top comments (0)