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:
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:
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.
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:
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.
Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.