Python print() Function

In this tutorial you will learn about python print() function, which is used to print objects and also about variables and parameters of the print function.

Python print() Function

Python print() Function

The print() function prints the given message to the screen of the output device, which can be a python interpreter, Terminal, or an IDE (integrated development environment), and the message can be a string or any other object like a list, tuple, set and dictionary.

Syntax of print() function

print(object(s), sep=separator, end=end, file=file, flush=flush)

Example 1:

print(“Hello World!”)

The output will be as follows.

Hello World!

Example 2:

print(“Hello World!”, ‘We are using Python3.’)

output :

Hello World! We are using Python3.

print() Function Parameters

  • Object: will print any object.
  • sep: will separate objects in more than one way. (Optional)
  • end: will specify what to print at the end. (Optional)
  • file: will print an object with a write method. (Optional)
  • flush: A Boolean specifying if the output is flushed (True) or buffered (False). (Optional)

print() Function with sep Parameter

The sep parameter stands for a separator which is used to separate the output of an object. The default value in the sep parameter is ‘  ‘.


print(“String1”,‘String2’,‘String3’, sep = ‘/’)
print(“String1”,‘String2’,‘String3’, sep = ‘-‘)
print(“String1”,‘String2’,‘String3’, sep = ‘0’)
print(“String1”,‘String2’,‘String3’, sep = ‘#’)

When we run the above program, we will get the following output.

String1 String2 String3

As we can see, we are using different symbols in a sep parameter, and it is replacing space with that specific symbol we used in the sep parameter.

print() Function with end Parameter

This parameter is used to specify the end of the printing object.

Example Python print variable:

print(“Hello World”, end=‘!’)
print(“Python3 is Awesome”,end=‘;’)

And the output will be as follows.

Hello World!
Python3 is Awesome;

print() Function with file Parameter

In python, we can print the content inside a file.

For better understanding, read Python File I/O (link here)

my_file = open(‘python.txt’, ‘w’)
print(‘Hello World!’, file = my_file)

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

Here, we have passed the my_file 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.