Take control of your Python print() statements: part 2

In the last post, we learned how to use .format() to improve the quality of our print() statements. You may have noticed that we were still printing lots of values after the decimal point, more than are necessary. In this post we will learn to control the presentation of numerical values.

Representing and adjusting numbers using .format()

With the .format() string method, we can specify the type of number that will be used. Specifically, we can insert :d in the curly brackets when the number is an integer, and we can insert :f when the number is a float (i.e., has a decimal point).

Here is a simple example:

# Integer
print('This will print {:d}, an integer number.'.format(125))

# float
print('This will print {:f}, a float number.'.format(1.25))

The first print statement produces: This will print 125, an integer number.

The second print statement produces: This will print 1.250000, a float number.

Do you notice something a little odd about the second print statement? It has printed the float number with 6 values after the decimal points, adding 4 zeros to our number.

Similar to printf from other programming languages (and older versions of Python), .format() gives us the ability to specify the total number of spaces that will be taken up by our number, as well as the number of digits after the decimal point. Here are a few examples:

from math import pi

print('Pi with 6 total spaces, including 5 digits after the decimal point {:6.5f}'.format(pi))

print('Pi with 2 total spaces, including 1 digits after the decimal point {:2.1f}'.format(pi))

print('Pi with 8 total spaces, including 1 digits after the decimal point {:8.1f}'.format(pi))

The out of these three lines of code is as follows:

Pi with 6 total digits, including 5 digits after the decimal point 3.14159
Pi with 4 total digits, including 1 digits after the decimal point 3.1
Pi with 8 total digits, including 1 digits after the decimal point       3.1

Notice something odd about the last print statement? We have asked Python to print pi using 8 spaces, but allowing for only 1 digit after the decimal point. Because pi only has a single value before the decimal point, Python has filled in those spots before the digit 3 with empty spaces. What would have happened if we were trying to print the number 123456789.6666? Similarly, what would happen if you ran the following print statement: print('{:5.5f}'.format(12345))? Try to run these variations and make sure you understand the output.

Conclusion

In this post we learned how to control the width and precision of the numbers we print using the .format() string method. We now have most of the tools we need to generate informative and well formatted print statements. In the last post of this Python print() series, we will learn to align our text and print items using a for loop. These additional tools will allow us to generate professional looking tables.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s