The purpose of this program is to print a well-formed price list according to the specified width.
Here‘s the code:
width = int(input('Please enter width:'))
price_width = 10
item_width = width -price_width
header_fmt = "{{:{}}}{{:>{}}}".format(item_width, price_width)
fmt = "{{:{}}}{{:>{}.2f}}".format(item_width, price_width)
print('=' * width)
print(header_fmt.format('Item', 'Price'))
print('-' * width)
print(fmt.format('Apples', 0.4))
print(fmt.format('Pears', 0.5))
print(fmt.format('Cantaloupes', 1.92))
print(fmt.format('Dried Apricots(16 oz.)', 8))
print(fmt.format('Prunes(4 lbs.)', 12))
print('=' * width)
Here are the questions:
What do the curly braces in
'{{:{}}}{{:>{}}}'.format(item_width, price_width)
mean?Where are item_width and price_width finally inserted?
In
print(header_fmt.format('Item', 'Price'))
, where are Item and Price inserted when I have already inserted item_width and price_width like those in the previous problem?
Ps: When I try to find the exact position that stores the inserted items like item_width, price_width ,Item and Price,I delete one pair of the curly braces, and it shows me TypeError
.
I am just wondering the meaning of each pair of curly brace?
In addition,I want to know what happened while formatting header_fmt again after item_width
and price_width
?
CodePudding user response:
What do the curly braces in
'{{:{}}}{{:>{}}}'.format(item_width, price_width)
mean?
Here's what the Python documentation has to say:
Format strings contain “replacement fields” surrounded by curly braces
{}
. Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. If you need to include a brace character in the literal text, it can be escaped by doubling:{{
and}}
.
In other words, each {}
is a "replacement field", which will be substituted with one of the arguments to format()
, in order. {{
and }}
are single braces ({
and }
, respectively); the reduplication is necessary so that they don't get misinterpreted.
Note that the interpretation of the format string is left-to-right, as with the rest of the format string processing. That affects the way }}}
is interpreted in the format string you show. Because there is an open {
at that point, the first }
is treated as ending the replacement field. The other two }}
become a single }
. So the interpretation of the format string is as follows:
{{ Literal {
: Literal :
{} Replacement field (item_width)
}} Literal }
{{ Literal {
:> Literal :>
{} Replacement field (price_width)
}} Literal }
The intention is to build a format string for later use. If item_width
were 12 and price_width
were 7, for example, the result would be:
{:12}{:>7}
which is a format string consisting of exactly two replacement fields and no other text. When that format string is then used with print(header_fmt.format('Item', 'Price'))
, it produces:
Item Price
Here, the string Item
is placed at the left of a field of 12 characters (so it is followed by 8 spaces), and Price
is placed at the right (because of the >
) of a field of 7 characters (so it is preceded by 2 spaces).
The documentation for format strings can be found in the Python reference manual (which is where the above quote comes from). However, you might want to start by reading the section on formatting in the Python tutorial which may be easier to understand, and which includes examples.