print() in Python

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Video Tutorial

Printing Multiple Values

Overview

The print() is standard Python function for printing messages to the console or to standard output device or to a file.

Syntax of print() in Python

The syntax of print() function is as follows:

Parameters of print() in Python

These are the parameters accepted by the print() function,

  • values: Any number of values can be given separated by commas.
  • separator: Used to specify how to separate the objects or values if it is more than one. By default, it is a whitespace character.
  • end: Used to specify what has to be printed in the end. By default, it is '\n'.
  • file: It must be an object with the write(string) method. If this option is not provided, sys.stdout will print objects to the console.
  • flush: A Boolean value indicating whether the output is flushed (True) or buffered (False). The default value is False.

sep, end, file, and flush are keyword arguments and are optional parameters in print() in Python.

Return Value of Python print() Function

Return Type: None

print in Python doesn't return any value.

Example:

Output:

What is print in Python?

A fundamental part of programming is printing the output. There are different methods for printing output in every programming language, whether to the console or to files. So, we have the print() function for this purpose.

The print() function works only in Python3 and not in Python2. In Python2, print statement is used. Note the difference in parenthesis.

Flush Argument

The I/O in Python is generally buffered, which means they are used in chunks. Until a display device is ready, data ready to be seen is held in an output buffer in memory or cache. It means that you might not see the output from your code right away, which makes debugging more difficult.

Here, a flush comes in handy since it allows users to decide whether they want the written content buffered or not. By default, it is false which means the output will generally be buffered.

More Examples of print() in Python

Example 1: print() with Separator and End Parameters in Python

Output:

Explanation:

  • Here, we've passed the sep and end parameters in the above program.
  • Within a print statement, a separator creates partitions between various objects/items. This attribute has by default a value of a whitespace character. Here, we've used *** as the separating value.
  • The end parameter configures the value you want to put at the end of all provided values. By default, it is '\n' i.e., newline character. In the above example, we've used two newline characters. Hence, the next print() statement prints after two lines.

Example 2: print() with File Parameters

In Python, the output can be written to a file using the file parameter. If the file doesn't exist, then it creates one with the same name and then writes to it.

Explanation:

  • Here, we have passed requiredFile file object to the file parameter. The string object 'I m writing to the file' is printed to the python.txt file.
  • Finally, the file was closed using the close() method.

Example 3: Using end Parameters

The end parameter configures the value you want to put at the end of the provided values. By default, it is '\n' i.e., newline character.

Output:

Explanation

  • In the above example, the print() uses the end parameter as '&'.
  • The value of end parameter can be noticed as, when the string1 ends the next print function is printing the string2 after the & .

Conclusion

  • We have just reached the end of this article on "Print in Python". In simple terms, the print() in Python is the function used for printing the outputs to the console or file.
  • It covers how print() in Python works along with various examples. The article also covers how print() in Python takes several parameters that are of great importance.