Print values using the print function
In , the built-in function print was used to print values to the screen. You will use the print function to display messages to the program’s users. Those messages may include the values that expressions produce and the values that the variables refer to. Here are two examples of printing:
| | >>> print(1 + 1) |
| | 2 |
| | >>> print("The Latin 'Oryctolagus cuniculus' means 'domestic rabbit'.") |
| | The Latin 'Oryctolagus cuniculus' means 'domestic rabbit'. |
Function print doesn’t allow any styling of the output: no colors, no italics, no boldface. All output is plain text.
The first function call performs as expected, given the numeric examples you have seen previously. Still, the second does something slightly different from previous string examples: it strips off the quotes around the string. It shows you the string in a human-readable form, rather than its character representation. This example makes the difference between the two even clearer:
| | >>> print('In 1859, Charles Darwin revolutionized biology') |
| | In 1859, Charles Darwin revolutionized biology |
| | >>> print('and our understanding of ourselves') |
| | and our understanding of ourselves |
| | >>> print('by publishing "On the Origin of Species".') |
| | by publishing "On the Origin of Species". |
The following example demonstrates that when Python prints a string, any escape sequences are converted to a form that humans can easily interpret:
| | >>> print('one\ttwo\nthree\tfour') |
| | one two |
| | three four |
The previous example illustrates how the tab character (\t) can be used to align values in columns.
In , you saw that \n indicates a new line in multiline strings. When a multiline string is printed, those \n sequences are displayed as new lines:
| | >>> numbers = '''one |
| | ... two |
| | ... three''' |
| | >>> numbers |
| | 'one\ntwo\nthree' |
| | >>> print(numbers) |
| | one |
| | two |
| | three |
Function print takes a comma-separated list of values and prints the values with a single space between them and a newline after the last value:
| | >>> print(1, 2, 3) |
| | 1 2 3 |
| | >>> |
When called with no arguments, which is a comma-separated list of length zero, print ends the current line and advances to the next one:
| | >>> print() |
| | |
| | >>> |
Function print can print values of any type, and it can even print values of different types in the same function call:
| | >>> print(1, 'two', 'three', 4.0) |
| | 1 two three 4.0 |
As with other function calls, it is also possible to call print with an expression as an argument. It will print the value of that expression:
| | >>> radius = 5 |
| | >>> print("The diameter of the circle is", radius * 2, "cm.") |
| | The diameter of the circle is 10 cm. |
Function print has a few extra helpful features; here is the help documentation for it:
| | >>> help(print) |
| | Help on built-in function print in module builtins: |
| | |
| | print(*args, sep=' ', end='\n', file=None, flush=False) |
| | Prints the values to a stream, or to sys.stdout by default. |
| | |
| | sep |
| | string inserted between values, default a space. |
| | end |
| | string appended after the last value, default a newline. |
| | file |
| | a file-like object (stream); defaults to the current sys.stdout. |
| | flush |
| | whether to forcibly flush the stream. |
The parameters sep, end, file, and flush have assignment statements in the function header! These are called default parameter values: by default, if you call the function print with a comma-separated list of values (known as positional arguments and denoted in the docstring as *args), the separator is a space; similarly, a newline character appears at the end of every printed string. (We won’t discuss file and flush; they are beyond the scope of this text.)
You can supply different values by using keyword arguments. (In the Python documentation, these are often referred to explicitly as kwargs.) That’s a fancy term for assigning a value to a parameter name in the function call. Here, you separate each value with a comma and a space instead of just a space by including sep=’, ’ as an argument:
| | >>> print('a', 'b', 'c') # The separator is a space by default |
| | a b c |
| | >>> print('a', 'b', 'c', sep=', ') |
| | a, b, c |
Often, you’ll want to print information without starting a new line. To do this, use the keyword argument end=” to tell Python to end with an empty string instead of a new line:
| | >>> print('a', 'b', 'c', sep=', ', end='') |
| | a, b, c>>> |
Notice how the last prompt appeared right after the ’c’. Typically, end=” is used only in programs, not in the shell. Here is a program that converts three temperatures from Fahrenheit to Celsius and prints using keyword arguments:
| | def convert_to_celsius(fahrenheit: float) -> float: |
| | """Return the number of Celsius degrees equivalent to fahrenheit degrees. |
| | |
| | >>> convert_to_celsius(75) |
| | 23.88888888888889 |
| | """ |
| | |
| | return (fahrenheit - 32.0) * 5.0 / 9.0 |
| | |
| | print('80, 78.8, and 10.4 degrees Fahrenheit are equal to ', end='') |
| | print(convert_to_celsius(80), end=', \n') |
| | print(convert_to_celsius(78.8), end=', and ') |
| | print(convert_to_celsius(10.4), end=' Celsius.\n') |
Here’s the output of running this program:
| | 80, 78.8, and 10.4 degrees Fahrenheit are equal to 26.666666666666668, |
| | 26.0, and -12.0 Celsius. |