Both explicit formatting options discussed in the previous theory page practically use the same syntax to determine how to display data. Python provides many different standardized formatting options for the string formatting syntax.
The formatting shorthands to be used with the string formatting techniques is almost its own sublanguage. The syntax follows the same structure for both string formatting options.
#\mathtt{\{}#[#\mathtt{\text{<reference>}}#][#\mathtt{!}##\mathtt{\text{<conversion>}}#][#\mathtt{:}##\mathtt{\text{<format>}}#]#\mathtt{\}}#
The #\mathtt{\text{<reference>}}# portion is optional when using #\mathtt{\color{#4271ae }{\text{format}}\text{()}}#, and should be equal to the name of the variable to be formatted. The #\mathtt{\text{<conversion>}}# portion is optional in both string formatting techniques and determines what to convert the variable to, default is #\color{#4271ae} {\mathtt{\text{str}}}#. The #\mathtt{\text{<format>}}# specifier determines an optional additional formatting style.
The #\mathtt{\text{<conversion>}}# field is rarely used, because the default usually suffices and in many cases, the result is generally the same regardless of which conversion is used. But for the sake of completeness it is important that you are aware that you do have the option to change it.
#\color{#4271ae}{\mathtt{\text{!s}}}#
#\color{#4271ae}{\mathtt{\text{!r}}}#
#\color{#4271ae}{\mathtt{\text{!a}}}#
Converts using #\mathtt{\color{#4271ae}{\text{str}}\text{()}}#. Returns #\color{#4271ae}{\mathtt{\text{str}}}#.
Converts using #\mathtt{\color{#4271ae}{\text{repr}}\text{()}}#. Returns literal.
Converts using #\mathtt{\color{#4271ae}{\text{ascii}}\text{()}}#. Returns ascii code if defined, else literal.
However, when using objects with a different literal representation, there is an apparent difference:
>>> greeting = "Hi" >>> f"{greeting!s}, student!"
|
'Hi, student!'
|
>>> f"{greeting!r}, student!"
|
"'Hi', student!"
|
>>> f"{greeting!a}, student!"
|
"'Hi', student!"
|
The #\mathtt{\text{<format>}}# field provides a lot of additional functionality for both formatting options. The syntax can look a bit overwhelming.
#\mathtt{:}#<[[#\mathtt{\text{<fill>}}#]#\mathtt{\text{<align>}}#][#\mathtt{\text{<sign>}}#][#\mathtt{\text{#}}#][#\mathtt{\text{0}}#][#\mathtt{\text{<width>}}#][#\mathtt{\text{<group>}}#][#\mathtt{\text{.<prec>}}#][#\mathtt{\text{<type>}}#]>
Each of these subcomponents has its own effect on the resulting formatting.
#\mathtt{\text{<fill>}}# |
Specifies which character is used to pad when the full #\mathtt{\text{<width>}}# is not used. |
#\mathtt{\text{<align>}}# |
Specifies how to align values when the full #\mathtt{\text{<width>}}# is not used. |
#\mathtt{\text{<sign>}}# |
Specifies whether a leading sign is included for numeric values. |
#\mathtt{\text{#}}# |
Selects an alternate output form for certain types. |
#\mathtt{\text{0}}# |
Pads values with zeroes from the left. |
#\mathtt{\text{<width>}}# |
Determines the minimum width of the resulting string in number of characters. |
#\mathtt{\text{<group>}}# |
Specifies a grouping character for numeric values. |
#\mathtt{\text{.<prec>}}# |
Specifies number of decimals after the decimal point for #\color{#4271ae} {\mathtt{\text{float}}}# and maximum width for #\color{#4271ae} {\mathtt{\text{str}}}#. |
#\mathtt{\text{<type>}}# |
Specifies the presentation type of the corresponding value. |
#\mathtt{\text{<fill>}}# specifies which character is used to pad when the full #\mathtt{\text{<width>}}# is not used, #\mathtt{\text{<fill>}}# can be any character except curly braces. If you specify a value for #\mathtt{\text{<fill>}}#, you have to specify a value for #\mathtt{\text{<align>}}#, #\mathtt{\text{<align>}}# can be any of the following characters: #\mathtt{\text{<}}#, #\mathtt{\text{>}}#, #\mathtt{\text{^}}#, or #\mathtt{\text{=}}#.
If #\mathtt{\text{<align>}}# is set to #\mathtt{\text{<}}#, values will be left-aligned. All examples use #\mathtt{\text{<fill>}}# with #\mathtt{\text{*}}# and a #\mathtt{\text{<width>}}# of #\color{#F5871F} {\mathtt{\text{10}}}#.
>>> var = 123 >>> f"{var:*<10}"
|
'123*******'
|
In contrast, #\mathtt{\text{>}}# aligns values to the right.
>>> f"{var:*>10}"
|
'*******123'
|
Using #\mathtt{\text{^}}# centers values within the given #\mathtt{\text{<width>}}#, if the value can't be centered exactly it will be shifted to the left by one character.
>>> f"{var:*^10}"
|
'***123****'
|
If align is set to #\mathtt{\text{=}}#, it will align values to the right in the same way #\mathtt{\text{>}}# does, but if #\mathtt{\text{<sign>}}# is set or if the number already includes a sign, it aligns the sign of the number to the left.
>>> f"{var:*=10}"
|
'*******123'
|
>>> var = -123 >>> f"{var:*=10}"
|
'-******123'
|
You can specify #\mathtt{\text{<align>}}# without specifying #\mathtt{\text{<fill>}}#, which will result in padding with whitespace. If you specify a #\mathtt{\text{<width>}}# without specifying #\mathtt{\text{<align>}}#, values are aligned to the right by default.
>>> var = 123 >>> f"{var:^5}"
|
' 123 '
|
Alternatively, the #\mathtt{\text{0}}# specifier can be used to pad values with zeroes. The #\mathtt{\text{0}}# has to be set after the #\mathtt{\text{<align>}}# flag. Using #\mathtt{\text{<fill>}}# and #\mathtt{\text{0}}# will result in the #\mathtt{\text{<fill>}}# character being used for padding.
>>> f"{var:010}"
|
'0000000123'
|
>>> f"{var:_>010}"
|
'_______123'
|
While rarely used, the #\mathtt{\text{#}}# specifier can be used to transform certain values. The three options are: binary representation #\mathtt{\text{#b}}#, octal representation #\mathtt{\text{#o}}#, and hexadecimal representation #\mathtt{\text{#x}}#.
>>> var = 123 >>> f"{var:#b}"
|
'0b1111011'
|
The #\mathtt{\text{<group>}}# specifier can be used to separate groups of three digits in numeric output. You can either use #\mathtt{\text{,}}# or #\mathtt{\text{_}}# as separator..
>>> var = 1000000 >>> f"{var:,}"
|
'1,000,000'
|
>>> f"{var:_}"
|
'1_000_000'
|
#\mathtt{\text{.<prec>}}# specifies the number of digits included after the decimal point for decimal representations. For strings,#\mathtt{\text{.<prec>}}# specifies the maximum width of the resulting output.
>>> var = 1.23456789 >>> f"{var:.2}"
|
'1.2'
|
>>> var = "Hello, world!" >>> f"{var:.5}"
|
'Hello'
|
The #\mathtt{\text{<type>}}# subcomponent is the most powerful within the format syntax, it determines the presentation type, i.e. how the value is transformed before it's displayed. Python supports many such representations. A #\mathtt{\text{ValueError}}# is returned when a value doesn't support a certain representation type.
#\mathtt{\text{c}}#
|
Transforms to corresponding unicode character.
|
#\mathtt{\text{d}}#
|
Transforms to decimal integer.
|
#\mathtt{\text{e}}#, #\mathtt{\text{E}}#
|
Shows exponential notation.
|
#\mathtt{\text{f}}#, #\mathtt{\text{F}}#
|
Transforms to floating point.
|
#\mathtt{\text{g}}#, #\mathtt{\text{G}}#
|
Chooses either exponential or floating point representation, depends on magnitude and #\mathtt{\text{.<prec>}}#.
|
#\mathtt{\text{s}}#
|
Shows a string.
|
#\mathtt{\text{%}}#
|
Transforms to percentage.
|
#\mathtt{\text{b}}#
|
Transforms to binary representation.
|
#\mathtt{\text{o}}#
|
Transforms to octal representation.
|
#\mathtt{\text{x}}#, #\mathtt{\text{X}}#
|
Transforms to hexadecimal representation.
|
All of the discussed subcomponents can be combined in the order specified above.
>>> var = 1000000 >>> f"{var:.1e}"
|
'1.0e+06'
|
>>> var = 1000000 >>> f"{var:*>+10g}"
|
'***+1e+06'
|