Kodeclik Blog

Python print()

Python’s print() is a versatile function that provides various options to format and style the output from your programs.

How the Python print() function works

The first thing to note about Python’s print() is that it is a function which means you need to pass it arguments. For instance:
This yields the output:
If we do:
We get:
Note that the second print() command prints its output on a new line. Calling print() without any arguments yields a blank line. For instance, the following segment of code:

Printing multiple information elements in print()

We can create a composite statement by printing a combination of strings using the string concatenation (+) operator.
This yields the output:
Note that we have included extra spaces around “is” and before “years old” in order to obtain the formatted string. (See also our detailed blogpost on the Python concatenate operator.)
Note carefully that the variable year has been defined as a string. If you try it like this:
You will get the error:
The reason for the error is that the concatenation operator requires strings on both sides of the operation. There are two ways to fix this. One is to convert the integer into a string using the str() function.
Another is to keep the year as integer and instead use print() as follows:
This will yield the output:
Note that there are extra spaces included if we use year as a separate argument in the print() function. We should therefore update the program as:

Separators in Python print()

When you use the multiple argument version of print() there are additional options available. For instance, you can do:
This gives:
The sep argument denotes the separator. In this case we are telling print() to use the string “--” as a separator (instead of the default single space). So if you wanted to mimic the effect of the “+” operator, you make sep to be an empty string.
This gives:
Using separators is particularly useful if you are printing your output to a file that will later be read by a program like Excel so that when importing the file you can specify the separator.

The end argument in Python print()

Just like the separator argument that specifies what should be printed *in between* the elements, there is an argument to specify what should be printed at the end. Recall that every print() statement starts on a newline. From this you can infer that the default end character is a newline. We can change it like so:
This gives:
Here is a place where this is useful. Suppose we would like to print all the months in a year with commas between them. One way to do it would be:
Note that, inside the for loop, we are printing all months except the last with an empty string as a separator (between the month and the comma), and then a space as an end character (before the next month is printed in the next print() statement inside the loop). Finally, we print the last month with a period at the end. This program uses all the elements we have discussed so far.
For more interesting content on Python printing, learn how to print special characters using the Python chr() function. Also checkout our blogpost on printing lists in Python.
Interested in more things Python? See our blogpost on Python's enumerate() capability, how to print to stderr, and how to read a text file in Python. Also learn all about computing square roots in Python and master recursion in Python!
Want to learn Python with us? Sign up for 1:1 or small group classes.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

Copyright @ Kodeclik 2024. All rights reserved.