Python | print function

The print() function outputs information on the screen. It can print on the screen any value from the basic data types. Place between parentheses whatever you want to be printed.

There are many differences between versions of Python. I use Python 2.X but there is Python 3.X already released. There are differences in the print() function.

Below is my hello.py file.

print('hello there!')
# Comment
# This is another comment
print('My name is Dan.')
print('4+3')
print(4+3)
print(7, 5+2)
print('4+3=',4+3)

This is what the terminal outputs.

Python print function

Everything in between quotes is printed as it is. If they are not placed in between quotes then it is executed. If you want to print more than one value and place a comma between the values then it prints everything after the keyword print, including the parentheses.

Look at the same code after I add from __future__ import print_function at the top of the file.

from __future__ import print_function
print('hello there!')
# Comment
# This is another comment
print('My name is Dan.')
print('4+3')
print(4+3)
print(7, 5+2)
print('4+3=',4+3)

The output would be like it is shown below.

Python 3 print function

The function from __future__ import print_function is used for compatibility with Python 3.

For example the line print('4+3=',4+3) will be printed on the screen as 4+3= 7. The comma is used to link the values you want to print on the screen and it also add a space between them.

In Python 2 the print is a statement but in Python 3 the print is a function.

Let’s say that you want to print quotes. In order to print double quotes you have to place then in between simple quotes.

If you want to print on multiple lines then you have to use the \n.

Print on multiple lines

Leave a Reply