F-String Template
f" Text{replacement_field} Text..."
f" Text{replacement_field} Text..."
Inside quotes, an f-string consists of two parts: (1) A regular string literal, i.e. text and (2) The replacement field
contains the Python expression for evaluation format and format control.
Double quotes are used here, but single quotes or Triple quotation marks.
F-string may contain only one replacement field: f”{replacement_field}”
Replace Field
{f-expression=!conversion:format_specifier}
A replacement field is represented by a pair of curly braces: { }
The replacement field consists of a Python expression for evaluation, with optional Debug mode (=), type conversion (!), and format specification (:).
Substitute into f-string template:
f" text{f expression=!conversion:format_specifier} text..."
Format Specifier
:fill align sign # 0 width sep .precision type
Format Specification Small Language Summary
filled with padding characters
align
: Alignment of text within spaces
sign
: + and - how to use
# : Alternative representation format for some numeric types
0 : Symbol awareness, zero padding for numbers width
: Minimum total field width September
: Number separator (',' or '_') .precision
: How many digits are displayed for floating point numbers; String maximum field width type
: According to data Representation type used by type
Notes : notation
, #, 0, September,Accuracy
and type
versus digital format is of particular interest, focus on this cheat sheet. For general handling of F-Strings, see my cheat sheet Python F-Strings Basics.
Format Specifier: Option
full
align
symbol
# 0
width
September
.prec
type
char
+
number
_
Number(s)
String
: s
>
-
,
number
: n
^
' '
Integer : d, b
,
o, x
, X
, C
=
float: e, E
, f
, F
, g, G, %
Signature and Delimiter Options
symbol
+
Signs are used for both positive and negative numbers
-
notation is only used for negative numbers ( default)
space
leading spaces for positive numbers and minus signs for negative numbers
separated Device
,
comma. Decimal Integers and Floating Points: Thousand Separator
_
underscore. Decimal Integers and Floating Points: Thousands Separator; b, o, x, and X Types: Every Four Digits Separator
Example: Symbols and Delimiters
#(+): add a sign to both positive and negative number
f"{3:+} or {-3:+}"
'+3 or -3 '
# sign (-): sign is used only for negative numbers
f"{3:-} or {-3: -}"
'3 or -3'
#Unsigned equals sign (-)
f"{3} or {-3}"
'3 or -3'
#Symbol (space): The leading space of a positive number and Negative sign for negative numbers
f"{3: } or {-3: }"
'3 or -3'
# Symbol (+ ), delimiter(,)
f"{10000 :+,}"
'+10, 000'
# padding(?), alignment(
f"{10000:?
'+10,000????'
Sign Aware Padding
Use =Alignment Option
to force padding determined by the symbol to be placed ( Padding and width specifications after the number if any) but before the number. This is useful for number representations like "+000042". This alignment option is only available for number types.
0-option
produces sign-aware zero-padding, Like the=alignment option padded with '0'. It is produced by prepending a '0' to the field width specification.
#fill(?), alignment(=), sign(+), width(5)
f"{3:?=+5}"
' +???3'
# full (0), alignment (=), sign (+), width (5)
f"{3:0=+5}"
'+0003'
# sign(+), 0-option, width(5)
f"{3:+05}"
'+0003'
# 0-options,width( 5)
f"{3:05}"
'00003 '
Integer representation type
Decimal integer type
d
Decimal Integer: Number in base 10
not any
as d
n
number. Same as d
, except that the current locale is used to set the appropriate number separator
other
b
Binary: A number in base 2.
o
Octal: A number in base 8.
x, X
Hexadecimal: a base 16 number. x
and X for lowercase and uppercase for numbers> 9
C
Unicode characters. Convert decimal integer to corresponding unicode character
#
-option: use# - option to add prefix
#b
: Add prefix 0b
#o
: Add prefix
0o
#x
: add prefix 0x
#X: add prefix 0X
Example: Integer Type
i=13
# Represents decimal 13 in binary, octal and hexadecimal
f"binary: {i:b}; octal: {i:o}; hex: {i:x} or {i:X}"
'Binary: 1101; Octal: 15; Hexadecimal: d or D'
# #-option Add prefix
f "binary: {i:#b }; octal: {i:#o}; hex: {i:#x} or {i:#X}"
' Binary: 0b1101; Octal: 0o15; Hex: 0xd or 0XD'
# Binary zero padding
f"{i:08b}"
'00001101'
# Binary file with prefix and zero padding
f"{i:#08b}"
'0b001101'
# fill (0), align (=), #-option, width (8), type (b)
f"{i:0=#8b}"
'0b001101'
# returns the decimal representation unicode character 36
f"{36:c}"
'$'
# print symbols 33-41 with unicode decimal representation
print(''.join([f"{x:c}" for x in range(33,42)]))
!"#$%&'()
# delimiter (,) and class Type(d)
f"{10000:,d}"
'10,000'
# type d is the default value for integers, so no need
f"{10000:,}"
'10,000'
#Separator(_),Type(b)
F"{ 1200:_b}"
'100_1011_0000 '
#Use the locale specific number format, set the locale
Import Locale
locale.setlocale(locale.LC_ALL, '')
# Then use format type 'n'
f"{106:n}"
'1,000,000'
Floating point representation type
type
Name
For a given precisionp
...
e, E
Scientific notation coefficients have 1 decimal point and p
digits after the decimal point.
f
, F
Fixed-point notation
Yesp digits after the decimal point.
G
, GGeneral format
round ds numbers are converted to p significant digits, and then according to the size in fixed point or Formatted in scientific notation. %
percentage Multiply the number by 100 and display it in fixed f format followed by a percent sign.
The default value for floating point numbers is p=6. Default value in decimal. The decimal p is large enough to display all coefficient digits.
Lowercase and uppercase: Make sure scientific notation is to display "e ' or 'E' ' and whether to use 'nan' versus 'NAN' and 'inf' versus 'INF'.
Example: Simple floating point comparison # %-style with precision=1
f"{1/2:.1 %}"
'50.0 %'
# lowercase e and uppercase Ef"{1000:.1e} with {1000:.1E}"
'1.0e+03 and 1.0E+03' #lowercase f and uppercase F
f"{1000:.1f} and {1000:.1F}"
'1000.0 vs 1000.0'
# lowercase g and uppercase GF "{1000:.1g} and {1000:.1G}"
'1e+03 and 1E+03'# Lowercase and uppercase NaN means nan=float('nan')
f"{nan: f} vs {nan:F}"
'nan vs NAN'
Replace Field
{f-expression=!conversion:format_specifier}
The replacement field consists of a Python expression for evaluation, with optional Debug mode (=), type conversion (!), and format specification (:).
Substitute into f-string template:
Substitute into f-string template:
f" text{f expression=!conversion:format_specifier} text..."
full |
align
|
|
|
, |
number |
number
_
Number(s)
String
number
Number(s)
String
Number(s)
String
: n
^
' '
Integer : d, b
,
o, x
, X
, C
=
float: e, E
, f
, F
, g, G, %
Signature and Delimiter Options
symbol
+
Signs are used for both positive and negative numbers
-
notation is only used for negative numbers ( default)
space
leading spaces for positive numbers and minus signs for negative numbers
separated Device
,
comma. Decimal Integers and Floating Points: Thousand Separator
_
underscore. Decimal Integers and Floating Points: Thousands Separator; b, o, x, and X Types: Every Four Digits Separator
Example: Symbols and Delimiters
#(+): add a sign to both positive and negative number
f"{3:+} or {-3:+}"
'+3 or -3 '
# sign (-): sign is used only for negative numbers
f"{3:-} or {-3: -}"
'3 or -3'
#Unsigned equals sign (-)
f"{3} or {-3}"
'3 or -3'
#Symbol (space): The leading space of a positive number and Negative sign for negative numbers
f"{3: } or {-3: }"
'3 or -3'
# Symbol (+ ), delimiter(,)
f"{10000 :+,}"
'+10, 000'
# padding(?), alignment(
f"{10000:?
'+10,000????'
Sign Aware Padding
Use =Alignment Option
to force padding determined by the symbol to be placed ( Padding and width specifications after the number if any) but before the number. This is useful for number representations like "+000042". This alignment option is only available for number types.
0-option
produces sign-aware zero-padding, Like the=alignment option padded with '0'. It is produced by prepending a '0' to the field width specification.
#fill(?), alignment(=), sign(+), width(5)
f"{3:?=+5}"
' +???3'
# full (0), alignment (=), sign (+), width (5)
f"{3:0=+5}"
'+0003'
# sign(+), 0-option, width(5)
f"{3:+05}"
'+0003'
# 0-options,width( 5)
f"{3:05}"
'00003 '
Integer representation type
Decimal integer type
d
Decimal Integer: Number in base 10
not any
as d
n
number. Same as d
, except that the current locale is used to set the appropriate number separator
other
b
Binary: A number in base 2.
o
Octal: A number in base 8.
x, X
Hexadecimal: a base 16 number. x
and X for lowercase and uppercase for numbers> 9
C
Unicode characters. Convert decimal integer to corresponding unicode character
#
-option: use# - option to add prefix
#b
: Add prefix 0b
#o
: Add prefix
0o
#x
: add prefix 0x
#X: add prefix 0X
Example: Integer Type
i=13
# Represents decimal 13 in binary, octal and hexadecimal
f"binary: {i:b}; octal: {i:o}; hex: {i:x} or {i:X}"
'Binary: 1101; Octal: 15; Hexadecimal: d or D'
# #-option Add prefix
f "binary: {i:#b }; octal: {i:#o}; hex: {i:#x} or {i:#X}"
' Binary: 0b1101; Octal: 0o15; Hex: 0xd or 0XD'
# Binary zero padding
f"{i:08b}"
'00001101'
# Binary file with prefix and zero padding
f"{i:#08b}"
'0b001101'
# fill (0), align (=), #-option, width (8), type (b)
f"{i:0=#8b}"
'0b001101'
# returns the decimal representation unicode character 36
f"{36:c}"
'$'
# print symbols 33-41 with unicode decimal representation
print(''.join([f"{x:c}" for x in range(33,42)]))
!"#$%&'()
# delimiter (,) and class Type(d)
f"{10000:,d}"
'10,000'
# type d is the default value for integers, so no need
f"{10000:,}"
'10,000'
#Separator(_),Type(b)
F"{ 1200:_b}"
'100_1011_0000 '
#Use the locale specific number format, set the locale
Import Locale
locale.setlocale(locale.LC_ALL, '')
# Then use format type 'n'
f"{106:n}"
'1,000,000'
Floating point representation type
type
Name
For a given precisionp
...
e, E
Scientific notation coefficients have 1 decimal point and p
digits after the decimal point.
f
, F
Fixed-point notation
Yesp digits after the decimal point.
G
, GGeneral format
round ds numbers are converted to p significant digits, and then according to the size in fixed point or Formatted in scientific notation. %
percentage Multiply the number by 100 and display it in fixed f format followed by a percent sign.
The default value for floating point numbers is p=6. Default value in decimal. The decimal p is large enough to display all coefficient digits.
Lowercase and uppercase: Make sure scientific notation is to display "e ' or 'E' ' and whether to use 'nan' versus 'NAN' and 'inf' versus 'INF'.
Example: Simple floating point comparison # %-style with precision=1
f"{1/2:.1 %}"
'50.0 %'
# lowercase e and uppercase Ef"{1000:.1e} with {1000:.1E}"
'1.0e+03 and 1.0E+03' #lowercase f and uppercase F
f"{1000:.1f} and {1000:.1F}"
'1000.0 vs 1000.0'
# lowercase g and uppercase GF "{1000:.1g} and {1000:.1G}"
'1e+03 and 1E+03'# Lowercase and uppercase NaN means nan=float('nan')
f"{nan: f} vs {nan:F}"
'nan vs NAN'
^ |
' ' |
|