Python print()

The print() function prints the given object to the standard output device (screen) or to the text stream file.


message = 'Python is fun'

# print the string message print(message)
# Output: Python is fun

print() Syntax

The full syntax of print() is:

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

print() Parameters

  • objects - object to the printed. * indicates that there may be more than one object
  • sep - objects are separated by sep. Default value: ' '
  • end - end is printed at last
  • file - must be an object with write(string) method. If omitted, sys.stdout will be used which prints objects on the screen.
  • flush - If True, the stream is forcibly flushed. Default value: False

Note: sep, end, file, and flush are keyword arguments. If you want to use sep argument, you have to use:

print(*objects, sep = 'separator')


print(*objects, 'separator')

print() Return Value

It doesn't return any value; returns None.

Example 1: How print() works in Python?

print("Python is fun.")

a = 5
# Two objects are passed
print("a =", a)
b = a # Three objects are passed
print('a =', a, '= b')


Python is fun.
a = 5
a = 5 = b

In the above program, only the objects parameter is passed to print() function (in all three print statements).


  • ' ' separator is used. Notice the space between two objects in the output.
  • end parameter '\n' (newline character) is used. Notice, each print statement displays the output in the new line.
  • file is sys.stdout. The output is printed on the screen.
  • flush is False. The stream is not forcibly flushed.

Example 2: print() with separator and end parameters

a = 5
print("a =", a, sep='00000', end='\n\n\n')
print("a =", a, sep='0', end='')


a =000005

a =05

We passed the sep and end parameters in the above program.

Example 3: print() with file parameter

In Python, you can print objects to the file by specifying the file parameter.

Recommended Reading: Python File I/O

sourceFile = open('python.txt', 'w')
print('Pretty cool, huh!', file = sourceFile)

This program tries to open the python.txt in writing mode. If this file doesn't exist, python.txt file is created and opened in writing mode.

Here, we have passed sourceFile file object to the file parameter. The string object 'Pretty cool, huh!' is printed to the python.txt file (check it in your system).

Finally, the file is closed using the close() method.

Also Read:

Did you find this article helpful?