Python | Learning the parameters of the print() function


Hello everyone. This page is an English translation of a Japanese page. (The original Japanese has been slightly rewritten to make it easier to translate into English.)

In this article, we learned about Python’s built-in function print() and how to use its parameter. This article is a reminder of what we learned.

The following is a brief description of the environment in which Python runs.

  • Windows 10 64bit
  • python 3.9.4
  • PyCharm Community Edition 2021.1 x64

More details can be found in previous article.

Check the definition of the print() function.

The Python 3.9.4 documentation provides the following definition for the print() function.

  • print(*objects, sep=’ ', end=’\n’, file=sys.stdout, flush=False)

The variable described in the above parentheses is called parameter.

For each Parameter, we tried to use it.

Parameter: *objects

The parameter “*objects" is of the type var-positional. It seems that the symbol “*" is used to represent an arbitrary number of arguments.

When calling the print() function in a real program, the numbers and variables described in parentheses are called arguments.

I have created a sample program as follows.


print('aaa', 'bbbb', 'ccccc')

The first print() function contains one string as an argument; the second print() function contains three strings as arguments.

The following is the result of the execution.

Execution results
aaa bbbb ccccc

As shown above, multiple parameters can be specified for “*objects". If you only specify parameters for “*objects", they will be separated by half-width spaces in the output.

Parameter: sep

The print() function outputs a string separated by the value of the parameter sep.

In the definition of the print() function, sep is written in the following format

  • parameter = expression

Such a function is said to have default parameter values.

The parameter defined in the above format can omit the argument; since it is defined as sep=’ ', if the argument is omitted, the half-width space will be the value of sep. In the above sample program, “aaa", “bbbb", and “cccc" are output separated by half-width spaces because the parameter is omitted.

We have created a sample program as follows.

print('aaa', 'bbbb', 'ccccc', sep=',')

I tried specifying ',’ (comma) for sep.

The following is the result of the execution.

Execution results

As specified in the argument, the strings are now output separated by commas.

Parameter: end

The print() function prints out the string specified by the argument, and finally the value of the parameter end.

The default parameter value “\n" is defined for the parameter end. This is a symbol that represents a new line. Therefore, if end is not specified in the argument, the output of the print() function will have a new line at the end.

We have created a sample program as follows.

print("If end is not specified")


print("If end is specified")
print("12345", end='')

In the print() function in line 9, the newline is not output because an empty character is specified for end.

The following is the result of the execution.

Execution results
If end is not specified

If end is specified

As shown above, the second “12345" is not followed by a new line; the output of print(“12345″, end=") does not break the line at the end of “12345", so the next “abcd" is printed on the same line.

Parameter: file

The print() function prints a string to the text stream specified by the parameter file. The text stream itself will not be explained here. A concrete example is the standard output.

In the parameter file, sys.stdout is specified as the default parameter value. This “sys" is one of the Python standard libraries. And “stdout" is the standard output. So, if file is not specified in arguments, the print() function will print a string to the standard output.

We have created a sample program as follows.

import sys

print('abc', file=sys.stderr)

In the above we have tried to specify sys.stderr as file. This represents the standard error output.

The following is the output result.

Execution results

On the PyCharm screen, the standard error output was printed in red. The order with the standard output seems to be different each time it is run. The above “abc" was sometimes output to the screen last.

Parameter: flush

The parameter flush is set to False as the default parameter value. With this value, the output seems to be stored as a buffer until a new line is created.

However, on the PyCharm screen, calling the print() function without a newline does not seem to hoard the buffer. On the other hand, Anaconda Prompt (Miniconda3) seems to hoard buffers when no newline is called.

We have created a sample program as follows.

import time

print('a', end="")
print('b', end="")
print('c', end="")
print('d', end="")
print('e', end="")


print(1, end="", flush=True)
print(2, end="", flush=True)
print(3, end="", flush=True)
print(4, end="", flush=True)
print(5, end="", flush=True)

The above time.sleep(1) is a command to wait for one second.

When we tried to run the above program with Anaconda Prompt (Miniconda3), the first character a through e were stored in a buffer and output at once. Also, for numbers 1 through 5, since we wrote flush=True, each was output every second.

For the print() function argument, the method of specifying an empty character for sep and True for flush is sometimes used when you want to display the progress.

That’s all. I hope this is helpful to you.