f-strings



[[fill]align][sign][#][0][width][grouping_option][.precision][type]

Literal curly braces

{ and } should be {{ and }} respectively.

>>> print(f"Literal curly braces: '{{' and '}}'")
Literal curly braces: '{' and '}'

Useful for debugging.

>>> a=3
>>> f"{a=}"
'a=3'

>>> f"{a}"
'3'

Sign

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

Alignment/padding

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):
  File "<stdin>", line 1, in <module>
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$

Grouping option

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

Conversion to string

Syntax Function
!r repr()
!s str()
!a ascii()

Alternate form (`#`)

# 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'

Presentation types (type)

Determines how the data is presented.

Integer

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

float

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%'

An example

>>> 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