The Print Function#
Learning Objectives#
- Write a print statement that prints a value or a single variable.
- Write a print statement that prints multiple values or variables.
- Use an f-string to print a string, substituting variables or expressions inside the string.
Overview of the Print Function#
Python has a built-in print
function. When working with data analytics, you will typically use print
for testing and debugging; as you perform an analysis, you may wish to print the value of a variable or expression. In this section, we will teach you the basics of printing. Please be aware that the print
function has many features and we will only show you a few of them. Therefore, do not be surprised if you look up something on the internet and discover a new feature of print
!
Printing a Single Value or a Variable#
To print a value, simply pass the value as an argument to print
:
print(-7)
-7
To print the value of a variable, pass the variable as an argument to print
:
pi = 3.14
print(pi)
3.14
When you pass an argument to print whose type is not string, print uses the str()
function to convert your argument to string before printing it.
Printing Multiple Values#
You can pass more than one argument to print. For example:
print(5, -3, 'sheep')
5 -3 sheep
Printing More Complicated Expressions#
Say you want to print something a little more complicated. For example, say you know someone’s name and age and want to print a sentence with that information. There are many crude ways of doing this and we will not show you those at this time. Instead, we will show you the most elegant way, which is f-strings. Let’s illustrate with an example:
name = 'Jack'
age = 23
print(f'Hello {name}. My records show that you are {23} years old.')
Hello Jack. My records show that you are 23 years old.
When Python sees the letter f before a string, it treats the string differently. Inside the string, it looks for expressions inside curly braces. It evaluates those expressions and replaces them with their values.
The expressions inside f-strings do not need to be variables. They can be more complicated. Here’s another example:
name = 'Jack'
age = 23
print(f'Hello {name.upper()}. In {40 - age} years, you will be 40!')
Hello JACK. In 17 years, you will be 40!
In the preceding example, the first expression inside the f-string was name.upper()
. The upper
function, which we will show you later, converts a string to upper case. The second expression 40 - age
evaluated to 17.
Summary of f-strings#
When Python sees the letter f before a string, it evaluates anything inside curly braces as an expression and replaces the expression with its value in the string.
f-strings are super useful and we strongly recommend their use. For a more thorough guide to f-strings, see here. That article also shows you the “old school” ways of string formatting in Python. If you look up code on the internet, chances are you will see the old-school ways of string formatting so keep the article as a reference!
Printing Formatted Numbers (OPTIONAL)#
You may find the material in this section useful when debugging, but it is optional.
We’re all accountants here. That means that when we print numbers, we most likely want either zero or two decimal places, and we want comma separators for large numbers! In this section, we will show you some basic number formatting.
Specifying the Number of Decimal Places#
unit_price = 7.00
print(f'The unit price is ${unit_price}')
The unit price is $7.0
The output from the previous example looks weird to us accountants. To print a number to a specified number of decimal places, add a colon (:) followed by a period (.), the number of decimal places, and the letter f. See code snippet below:
unit_price = 7.00
print(f'The unit price is ${unit_price:.2f}')
The unit price is $7.00
The colon following the expression tells Python that the expression has ended and now you are telling it about formatting. The f tells Python that the expression should be converted to a float. The .2 tells Python that you want two decimal places.
If the floating point number has more decimal places, it will be rounded to the number of decimal places you specified. Here’s a demonstration:
unit_price = 14.499999
print(f'The unit price is ${unit_price:.2f}')
The unit price is $14.50
Adding Commas#
To format a number with commas, simply add a comma after the colon. Here are a few examples:
revenue = 1000000
COGS = 0.63 * revenue
GrossMargin = revenue - COGS
print(f'Revenue {revenue:,}')
print(f'COGS {COGS:,.0f}')
print(f'Gross margin {GrossMargin:,.0f}')
Revenue 1,000,000
COGS 630,000
Gross margin 370,000
In the above example, there was no need to specify the number of decimal places for revenue because it was an integer. We specified 0 decimal places for COGS and gross margin.
Padding Numbers with Spaces#
Sometimes you may wish to add space to your printing so that your output is easier to read. To do so, you can enter a number before the decimal or comma in the format string. That number is the minimum width (in number of characters) of the value that will be printed. This will be easier to understand with an example:
revenue = 900000.0
for year in range(2010,2017):
print(f'{year} revenue: ${revenue:10,.0f}')
revenue = revenue * 1.50
2010 revenue: $ 900,000
2011 revenue: $ 1,350,000
2012 revenue: $ 2,025,000
2013 revenue: $ 3,037,500
2014 revenue: $ 4,556,250
2015 revenue: $ 6,834,375
2016 revenue: $10,251,562
In the above example, 2010 revenue was $900,000 and grew at an annual rate of 50%.
The format string, ‘10,.0f’ told Python to always print the revenue number using a minimum width of 10 characters. The 2016 revenue of 10,251,562 is 10 characters wide, but the revenue in previous years is not; therefore 2010 - 2015 revenues were “padded” by placing spaces to the left of the number so the width is exactly 10 characters. Since all years’ revenues are printed to the same width, the output is prettier.