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

In the last post, our program printed out the computed BMI with lots of precision (24.489795918367346) and no additional information. In hindsight, it would have been more informative to tell the user that A weight of 75 kg and a height of 1.75 meters equal a MBI if 24.5. In the next few posts we will learn how to create informative print() statements.

A basic example using .format()

Here is a basic script that divides a numerator by a denominator and prints the answer.

num = 1
denom = 3
val = num/denom

The output of this code is: 0.3333333333333333.

Let’s improve the output by using the .format() string method. A string is any type of text that is inputed between quotation marks (e.g., x = "This is a string"). A method is a Python function that belongs to specific class or data type (e.g., x.upper() results in "THIS IS A STRING" because the .upper() method converts all the letters in the variable x to upper-case).

Here is our new version using the .format() method:

print('{} divided by {} equals {}'.format(num, denom, val))

The output is: 1 divided by 3 equals 0.3333333333333333. Much more informative!

Let’s figure out how the .format() method works. The first part of the text in the print() function is in quotation marks, making it a string ('{} divided by {} equls {}'). You will note that this string contains three pairs of curly brackets (i.e., {}). The curly brackets are how we inform the .format() method where we want to add text. The second part of the text in the print() function calls the .format() method and the three variables we want to be printed. When used in this way, .format() fills the curly brackets sequentially.

We can specify the order the variables are printed in (although the answer is nonsensical in this example here):

print('{2} divided by {1} equals {0}'.format(num, denom, val)))

The output is: 0.3333333333333333 divided by 3 equals 1.

Remember that Python starts counting at zero. So {2} will be where the .format() method inserts the third variable provided, i.e., val. A similar logic explains why denom is inserted in the second pair of curly brackets and num in the third.

As a final variant, you can label the curly brackets. In this case, the order of the variables in .format() does not matter.

print('The numerator is {numerator} and the denominator is {denominator}.'.format(denominator=denom, numerator=num))

The output is: The numerator is 1 and the denominator is 3.


With the introduction of the .format() string method, we have learned how to generate more informative print statements. We learned to use double curly brackets to indentify the place in a string where we want to add text. In our examples, we specified the text to add by providing the name of variables (i.e., num, denom and val), but you can also provide numerical and text values (e.g., print('{} and {}'.format(1, 'hello'))). Give it a try and see what happens! Play around with the aspects of the .format() string method that we introduced here before moving on to the next lesson.


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