1.6 Plotting, Printing and Input Data
29
So, make sure the order of the arguments is correct. An alternative is to name the
arguments.
Naming the Arguments If we name the arguments (v1 and v2 are arguments to
format), we get the correct printout whether we call print as
print(’v1 is {v1}, \nv2 is {v2}’.format(v1=v1, v2=v2))
or, switching the order,
print(’v1 is {v1}, \nv2 is {v2}’.format(v2=v2, v1=v1))
Note that the names introduced do not have to be the same as the variable names,
i.e., “any” names would do. Thus, if we (for the sake of demonstration) rather use
the names a and b, any of the following calls to print would work just as fine (try
it!):
print(’v1 is {a}, \nv2 is {b}’.format(a=v1, b=v2))
or
print(’v1 is {a}, \nv2 is {b}’.format(b=v2, a=v1))
Controlling the printout like we have demonstrated this far, may be sufficient
in many cases. However, as we will see next, even more printing details can be
controlled.
Formatting More Details Often, we want to control how numbers are formatted.
For example, we may want to write 1/3 as 0.33 or 3.3333e-01 (3.3333 · 10 −1 ),
and as the following example will demonstrate, such details may indeed be specified
in the argument to print. The essential new thing then, is that we supply the
placeholders {} with some extra information in between the brackets.
Suppose we have a real number 12.89643, an integer 42, and a text ’some
message’ that we want to write out in the following two different ways:
real=12.896, integer=42, string=some message
real=1.290e+01, integer=
42, string=some message
The real number is first to be written in decimal notation with three decimals, as
12.896, but afterwards in scientific notation as 1.290e+01. The integer should first
be written as compactly as possible, while the second time, 42 should be placed in
a five character wide text field.
The following program, formatted_print.py, produces the requested output:
r = 12.89643
# real number
i = 42
# integer
s = ’some message’
# string
(equivalent: s = "some message")
print(’real={:.3f}, integer={:d}, string={:s}’.format(r, i, s))
print(’real={:9.3e}, integer={:5d}, string={:s}’.format(r, i, s))
Here, each placeholder carries a specification of what object type that will enter in
the corresponding place, with f symbolizing a float (real number), d symbolizing
an int (integer), and s symbolizing a str (string). Also, there is a specification of
how each number is to be printed. Note the colon within the brackets, it must be
there!
29
So, make sure the order of the arguments is correct. An alternative is to name the
arguments.
Naming the Arguments If we name the arguments (v1 and v2 are arguments to
format), we get the correct printout whether we call print as
print(’v1 is {v1}, \nv2 is {v2}’.format(v1=v1, v2=v2))
or, switching the order,
print(’v1 is {v1}, \nv2 is {v2}’.format(v2=v2, v1=v1))
Note that the names introduced do not have to be the same as the variable names,
i.e., “any” names would do. Thus, if we (for the sake of demonstration) rather use
the names a and b, any of the following calls to print would work just as fine (try
it!):
print(’v1 is {a}, \nv2 is {b}’.format(a=v1, b=v2))
or
print(’v1 is {a}, \nv2 is {b}’.format(b=v2, a=v1))
Controlling the printout like we have demonstrated this far, may be sufficient
in many cases. However, as we will see next, even more printing details can be
controlled.
Formatting More Details Often, we want to control how numbers are formatted.
For example, we may want to write 1/3 as 0.33 or 3.3333e-01 (3.3333 · 10 −1 ),
and as the following example will demonstrate, such details may indeed be specified
in the argument to print. The essential new thing then, is that we supply the
placeholders {} with some extra information in between the brackets.
Suppose we have a real number 12.89643, an integer 42, and a text ’some
message’ that we want to write out in the following two different ways:
real=12.896, integer=42, string=some message
real=1.290e+01, integer=
42, string=some message
The real number is first to be written in decimal notation with three decimals, as
12.896, but afterwards in scientific notation as 1.290e+01. The integer should first
be written as compactly as possible, while the second time, 42 should be placed in
a five character wide text field.
The following program, formatted_print.py, produces the requested output:
r = 12.89643
# real number
i = 42
# integer
s = ’some message’
# string
(equivalent: s = "some message")
print(’real={:.3f}, integer={:d}, string={:s}’.format(r, i, s))
print(’real={:9.3e}, integer={:5d}, string={:s}’.format(r, i, s))
Here, each placeholder carries a specification of what object type that will enter in
the corresponding place, with f symbolizing a float (real number), d symbolizing
an int (integer), and s symbolizing a str (string). Also, there is a specification of
how each number is to be printed. Note the colon within the brackets, it must be
there!
