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
,.p
ort
. -
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 withint
orfloat
input. *Padding is added between+
or-
and input.
-
-
s
(sign
) is+
,-
, or " " which can be used only withint
,float
orcomplex
input: *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 ifs
isn't set. -
" "
indicates that a leading space should be used on positive numbers, and a minus sign on negative numbers.
-
-
z
is to make a negative zero a positive zero. *z
can be used only withfloat
orcomplex
input. -
#
is to convert a string to other form. *#
can be used only withint
,float
orcomplex
input. -
0
is to fill the left and/or right side of the one or more available spaces of a string with0
. It's equivalent to thef
character0
. -
w
is to decide the width of a string: *Memos:- It must be
0 <= integer
if it's set. - If it's not set, alignment doesn't occur.
- It must be
-
g
(grouping
) is to separate digits with,
or_
. *g
can be used only withint
,float
orcomplex
input. -
.p
(.precision
) is to decide the scale or precision of a number or the number of the characters of a string: *Memos:- For
f
orF
, scale(the number of the digits after the decimal point) is decided. *Scale is displayed even if they are zero. - For
g
orG
, 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 withs
forstr
input,b
,c
,d
,o
,x
,X
orn
forint
input ore
,E
,f
,F
,g
,G
,n
or%
forfloat
,complex
and Decimal() input: *Memos:-
s
is string format. *Ift
isn't set forstr
input,t
is default tos
. -
d
is decimal integer. *Ift
isn't set forint
input,t
is default tod
. -
f
is fixed-point notation. *If.p
is set, scale is.p
digits while if.p
isn't set, scale is 6 digits and if.0
is set, scale is omitted unless#
is set. -
F
is same asf
except that uppercaseNAN
andINF
are used. -
g
is general format. -
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
. -
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('"{: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)

Super Kai (Kazuya Ito) | Sciencx (2025-07-11T00:13:01+00:00) String in Python (17). Retrieved from https://www.scien.cx/2025/07/11/string-in-python-17/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.