Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Practical difference between keyword "print" and function "print" in Python [duplicate]

Tags:

python

For Python2x If we use,

from __future__ import print_function

the print will become function which is previously the keyword

But practically,

How two of these are different ?

I can still use print the same way as I was using it when it was a keyword, then what difference does it make and how could it be more efficient than before ?

like image 613
Sufiyan Ghori Avatar asked Jan 16 '26 21:01

Sufiyan Ghori


1 Answers

Here you can read some official documentation with examples and PEP3105 with more details.

As quick differences, there are several things which are very easy to do with print function (Python 3 or from __future__ import print_function) or at least very consistently:

No newline

print('Hello!', end='')

That was more complicated in python 2.

Print to stderr

import sys
print('ERROR!!!!!', file=sys.stderr)

In python 2.

like image 199
Diego Herranz Avatar answered Jan 19 '26 19:01

Diego Herranz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!