1.7. Printing output#
In previous sections have used the print()
function to print the value of a variable to the console. This can be used to print a variety of data types, for example the command
print("hello world")
will print the text string hello world
. Lets print a short header that tells someone what our program does. Edit your program so that the following appears above the pi
variable declaration
print("Degrees to radians conversion")
print("-----------------------------")
Running your program you should see the following printed to the console.
Degrees to radians conversion
-----------------------------
0.785398175
Note that each time the print()
function is called the text is printed on a new line.
1.7.1. Printing text and numbers#
So now our program has three print commands, two that prints some text and another that prints a floating point number. Wouldn’t it be nice if we could print text and numbers at the same time. This is known as formatted output and we can do this by putting an f
between the opening bracket and the double quotes "
and a variable in curly brackets {...}
, i.e.,
print(f"some text {variable}")
Edit your program so that the following print commands replace the command used to print the angle in radians.
print(f"angle in degrees = {angle_in_degrees}")
print(f"angle in radians = {angle_in_radians}")
Running your program you should see the following printed to the console.
Degrees to radians conversion
angle in degrees = 45
angle in radians = 0.785398175
1.7.2. Format specifier#
In our program the angle in radians is printed using 9 decimal places. We probably don’t need this level of accuracy so lets reduce the number of decimal places outputted to 3 by editing the print commands so that they look like the following.
print(f"angle in degrees = {angle_in_degrees:6.3f}")
print(f"angle in radians = {angle_in_radians:6.3f}")
Running your program you should see the following printed to the console.
Degrees to radians conversion
-----------------------------
angle in degrees = 45.000
angle in radians = 0.785
Notice that the angles have now been printed using 3 decimal places. The 6.3f
is an example of a format specifier which controls how the values of the variables are printed. Here we have told Python to print a float value that uses a total of 6 character spaces (including the decimal point) with 3 significant figures following the decimal point.
The different types of format specifiers are shown in Table 1.4.
Data type |
Specifier |
Python code |
Output |
---|---|---|---|
integer |
|
|
|
float |
|
|
|
Scientific notation |
|
|
|
String |
|
|
|
If the number to the left of the decimal point is zero, Python will use the smallest number of spaces required to print the number, e.g., print(f"{1.23456:0.2f}")
will print 1.23
.
1.7.3. The newline character#
The newline character, \n
, is used to instruct Python to print the rest of the string on new line. For example
print("This text \nis printed\n\non multiple lines \n\n\nusing a single print command.")
will print
This text
is printed
on multiple lines
using a single print command.
Lets print the header using a single print()
command. Edit your program so that the first two print()
commands are replaced by the following.
print("Degrees to radians conversion\n-----------------------------")
Running your program you should see that the console output has not changed.
1.7.4. Escape characters#
Some characters cannot be contained within a string, e.g., "
. To print such a character we can use an escape character which is a backslash \
followed by the character we want to print. For example, to print
The feature film "Monty Python's Life of Brian" was released in 1979.
we could use
print(f"The feature film \"Monty Python's Life of Brian\" was released in 1979.")
1.7.5. Exercise#
Write a program that determines the number of years, weeks, days, minutes and seconds that are in a given number of seconds. Your program should print out the result as a single sentence that mixes text and numbers. For example, if the given number of seconds is 1 million, then your program should print
There are 0 years, 1 weeks, 4 days, 13 hours, 46 minutes and 40 seconds in 1000000 seconds.
Use your program to print the number of years, weeks, days, hours and minutes in 1 billion seconds.