https://docs.python.org/3/library/string.html#format-specification-mini-language
https://docs.python.org/3/library/string.html#format-examples
https://docs.python.org/3/reference/lexical_analysis.html#f-strings
https://docs.python.org/3/library/string.html#format-string-syntax
[[fill]align][sign][#][0][width][grouping_option][.precision][type]
{
and }
should be {{
and
}}
respectively.
>>> print(f"Literal curly braces: '{{' and '}}'")
'{' and '}' Literal curly braces:
Useful for debugging.
>>> a=3
>>> f"{a=}"
'a=3'
>>> f"{a}"
'3'
Only for numbers.
Syntax | Sign |
---|---|
+ |
Print sign for all numbers |
- |
Print sign only for negative numbers |
Space | Print sign for negative and space for positive |
z |
Only for floats. -0.0 will become 0.0 ?? |
z
option is only from v3.11.
More: #
, ,
, _
–
>>> print(f"{+3.1215926:+}")
+3.1215926
>>> print(f"{-3.1215926:+}")
-3.1215926
>>> print(f"{24:-}")
24
>>> print(f"{-24:-}")
-24
>>> print(f"{-24: }")
-24
>>> print(f"{24: }")
24
Will also work for `bool`s due to an implicit conversion to `int`:
>>> print(f"{True: }")
1
>>> print(f"{True:+}")
+1
But not for strings:
>>> print(f"{'hi':-}")
ValueError: Sign not allowed in string format specifier
Syntax | Align | Comment |
---|---|---|
< |
Left | default for numbers |
> |
Right | default for strings |
^ |
Center | |
= |
Padding before number but after sign | numbers only |
>>> s="hello"
>>> f"{s:^10}"
' hello '
>>> f"{s:10}"
'hello '
>>> f"{s}"
'hello'
# Filling
>>> f"{s:*^11}"
'***hello***'
>>> f"{s:=10}"
Traceback (most recent call last):"<stdin>", line 1, in <module>
File ValueError: '=' alignment not allowed in string format specifier
###
>>> msg = "hello"
>>> print(f"^{msg: <10}$")
^hello $
>>> print(f"^{msg: >10}$")
^ hello$
>>> print(f"^{msg: ^10}$")
^ hello $
>>> print(f"^{3: =+10}$")
^+ 3$
Available grouping options are:
,
)_
)Neither are locale aware.
For integers, _
can be used only with the
'b'
, 'o'
, 'x'
and
'X'
presentation types.
By default, the grouping is done by forming groups of three digits each from the right ????
>>> f"{123456789:,}"
123,456,789
>>> f"{12345678:_}"
12_345_678
Syntax | Function |
---|---|
!r |
repr() |
!s |
str() |
!a |
ascii() |
#
can be used to get an 'alternate form'. This is
defined differently for different types.
Valid only for integer, float and complex types.
Type | Alternate form |
---|---|
x |
Adds '0x' in front |
X |
Adds '0X' in front |
o |
Adds '0o' in front |
b |
Adds '0b' in front |
>>> f"{12:b}, {12:#b}"
'1100, 0b1100'
>>> f"{12:o}, {12:#o}"
'14, 0o14'
>>> f"{12:x}, {12:#x}"
'c, 0xc'
>>> f"{12:X}, {12:#X}"
'C, 0XC'
Determines how the data is presented.
s
)Type | Meaning |
---|---|
b |
binary format |
c |
Unicode character corresponding to integer |
d |
decimal format. Default presentation type |
o |
octal format. |
x |
hexadecimal format using lower-case letters |
X |
hexadecimal format using upper-case letters. |
n |
Like d , but inserts number separator based on
locale |
Presentation types of float can also be used with integers, in which case the number is first converted to a float with `float()` before the formatting is done.
>>> f"{78:b}"
1001110
>>> f"{78:c}"
N
>>> f"{78:d}"
78
>>> f"{78:o}"
116
>>> f"{78:x}"
4e
>>> f"{78:X}"
4E
>>> f"{78:n}"
78
Presentation types for float
and Decimal
values.
Option | Meaning |
---|---|
e |
Scientific notation with 'e' |
E |
Same as `'e'` but with 'E' |
f |
|
F |
Like f but nan , inf becomes
NAN , INF |
g |
|
G |
|
n |
|
% |
Multiplies value by 100 and shows in f format with a
'%' symbol |
None |
>>> f"{1234.56:e}"
'1.234560e+03'
>>> f"{1234.56:.2e}"
'1.23e+03'
>>> f"{1234.56:E}"
'1.234560E+03'
>>> f"{0.12:%}"
'12.000000%'
>>> f"{1.2:%}"
'120.000000%'
>>> f"{3.1415926:*>+#015_.4f}"
'********+3.1416'
where
Option | Value |
---|---|
Fill | '*' |
Align | > (right align) |
Sign | + (always display sign) |
Width | 15 |
Grouping | Underscore |
Precision | 4 digits |
Type | f |