Python | Learning the parameters of the print() function

2021-07-03

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.

Source
print('Hello')

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
Hello
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.

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

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

The following is the result of the execution.

Execution results
aaa,bbbb,ccccc

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.

Source
print("If end is not specified")
print("12345")
print("abcd")
print("ABCDEF")

print()

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

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
12345
abcd
ABCDEF

If end is specified
12345abcd
ABCDEF

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.

Source
import sys

print('123')
print('456')
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
abc
123
456

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.

Source
import time

print('a', end="")
time.sleep(1)
print('b', end="")
time.sleep(1)
print('c', end="")
time.sleep(1)
print('d', end="")
time.sleep(1)
print('e', end="")

print()

print(1, end="", flush=True)
time.sleep(1)
print(2, end="", flush=True)
time.sleep(1)
print(3, end="", flush=True)
time.sleep(1)
print(4, end="", flush=True)
time.sleep(1)
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.