Python print() Function Parameters Explained

print function parameters in Python

You may be familiar with the print() function that takes only one parameter, that is, the item that you want to print. In reality, the print() function takes additional 4 parameters.

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

The print function parameters are:

  1. *objects – an object/objects to be printed.
  2. sep – the separator between multiple printed objects.
  3. end – the character/string printed at the end after the object.
  4. file – specifies the file where the output goes. By default this is the console.
  5. flush – flushes the stream/file forcibly if set True

In the above, only the first argument *objects is mandatory. The rest of the arguments are not.

In this guide, you will learn what each of the print parameters means and when to use them.

Python print() Function Parameters

You have probably used Python’s print() function this way:

print("Hello world")

Output:

Hello world

Or even like this with an arbitrary number of arguments:

n1 = 1
n2 = 2
n3 = 3

print(n1, n2 ,n3)

Output:

1 2 3

But according to the official documentation, the print() function follows this syntax:

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

There seems to be 5 arguments that the print() function accepts. This may cause some confusion, so let me show you how what each of these arguments does, and when they are useful.

1. *objects

The *objects parameter represents an arbitrary number of arguments. This makes it possible to call the print() function any number of inputs. This is why there is an asterisk * in front of the argument name. If there was no asterisk, you could only print one element at a time.

Example usage:

print("Hello") # Print a single value
print(1, 2, 3) # Print multiple values 

2. sep

The separator parameter sep acts as a separator if the print() function is called with multiple values. By default, this is set an empty string, ‘ ‘.

If you want to change it, you need to specify the separator by providing the argument as a keyword argument in the print() function call. In other words, you have to add sep=’something’ into the print() function call.

For instance, let’s separate each element by two new lines:

print(1, 2, 3, sep='\n\n') 

Output:

1

2

3

The separator is useful if you want to customize the way multiple values are printed into the console.

3. end

The end parameter specifies the string/character to which the printed value ends. By default, it is a line break.

As you may know, calling multiple print() functions adds a new line automatically. This is because, by default, each print() function call adds a newline character ‘\n’ to the end of the row.

For instance:

print("Hello")
print("world")

Output:

Hello
world

If you do not want each print to cause a new line, you can change the value of the end parameter in the print() function call.

For instance, let’s use a blank space instead of a new line:

print("Hello", end=" ")
print("world")

Output:

Hello world

Tweaking the end parameter in the print() function can be useful if you want to change the way consecutive prints appear in the console.

4. file

The file parameter determines where the print() function sends the output. By default, it sends the output into the console.

By default, the print() function displays the output in the console. But this is just one stream where the output can be displayed. You can for example print the results into a separate text file. To do this, specify the file parameter.

For example, let’s print a string to a new file called example.py:

with open("example.txt", "w") as text_file:
    print("Hello, file!", file=text_file)

As a result, you will not see a print in the console. Instead, the string is printed to the file called example.txt in the same folder with your project.

5. flush

Flushing in Python means flushing the output stream buffer. If you want to flush the output stream when printing, you need to set flush argument True in the print() function call.

But why would you ever do something like that?

In Python, the input/output functions, such as print(), are expensive to call. To improve the performance, your operating system stores input/output data into a buffer before sending it to the stream, such as into the console.

When you print in Python, the data is piled up into a buffer until there is a line break. When a line break occurs, the printed data is sent to the console.

Sometimes this can cause headaches.

Let’s demonstrate this via an example where we build a count-down timer. This timer works such that it prints numbers 3, 2, and 1 one second apart from one another and then prints “Go” at the end like this:

3...2...1...Go

As you know, calling print() function automatically adds a line break at the end. To make the counter work, however, you do not want line breaks. Instead, you want to display between each second, so you need to replace the automatically added line break with three dots.

Here is how you would probably implement the counter described above:

import time

s = 3

for countdown in reversed(range(s + 1)):
    if countdown > 0:
        print(countdown, end="...")
        time.sleep(1.0)
    else:
        print("Go")

But if you run this program, you notice it waits for 3 seconds and only then prints the whole thing in one go.

3...2...1...Go

This is not what we want.

This problem occurs because of the absence of line breaks. As you learned, the operating system optimizes the print() calls by buffering them until it sees a line break. The first line break in the above code happens only on the last line print(“Go”). Until this, the buffer stores all the printed items, and nothing appears in the console.

To overcome this issue, you need to tell the operating system not to buffer subsequent print() function calls. In other words, you want to flush the buffer every time you call print().

To do this, simply set flush=True into the print() function call:

import time

s = 3

for countdown in reversed(range(s + 1)):
    if countdown > 0:
        print(countdown, end="...", flush=True)
        time.sleep(1.0)
    else:
        print("Go")

Now if you run the program, you will see the countdown timer work as expected.

Conclusion

Today you learned about the parameters of the print() function in Python.

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

To recap:

  • *objects represents an arbitrary number of objects you want to print.
  • sep acts as the separator between multiple printed objects. It is a blank space by default.
  • end determines the last character after printing. By default it is a line break.
  • file specifies the output stream, that is, where the printed items are displayed. By default it is the console.
  • flush enables/disables the buffering optimization with consecutive print() calls that do not have line breaks.

Thanks for reading.

Happy coding!

Further Reading

Python Tricks

How to Write to a File in Python

The with Statement in Python

Share on facebook
Share on twitter
Share on linkedin

Leave a Comment

Your email address will not be published.