Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
closed by

I comprehend what print does, however of what "type" is that language component? I believe it's a capacity/function, yet for what reason does this fall flat? 

>>> print print

SyntaxError: invalid syntax

Isn't print a capacity/function? Shouldn't it print something like this?

>>> print print

<function print at ...>

closed

4 Answers

0 votes
by (25.7k points)
 
Best answer
In Python 3, print is not a regular function; it is a statement. In earlier versions of Python (2.x), print was indeed a function. This distinction is why you encounter a SyntaxError when attempting to use the syntax print print.

To understand the change, consider the following:

In Python 2.x:

print print

Output:

<built-in function print>

In Python 3.x:

print(print)

Output:

<built-in function print>

In Python 3, print is treated as a statement rather than a function. Therefore, using print print would result in a SyntaxError because it violates the syntax rules of Python 3.

If you wish to obtain the string representation of the print function in Python 3, you need to pass it as an argument to the print() function, as shown in the example above.
0 votes
by (26.4k points)

In 2.7 and down, print is an assertion. In python 3, print is a function. To utilize the print work in Python 2.6 or 2.7, you can do 

>>> from __future__ import print_function

>>> print(print)

<built-in function print>

See this segment from the Python Language Reference, just as PEP 3105 for why it changed.

Want to learn python to get expertise in the concepts of python? Join python certification course and get certified

0 votes
by (15.4k points)
In Python 3, the print statement has been changed to a print() function. However, in earlier versions like Python 2.x, print was a statement. As a result, if you try to use the syntax print print in Python 3, it will raise a SyntaxError. In Python 3, to obtain the string representation of the print function, you can use print(print), which will output <built-in function print>.
0 votes
by (19k points)
In Python 3, the print statement has been transformed into a built-in function called print(). However, in earlier versions of Python (such as Python 2.x), print was used as a standalone statement. Consequently, attempting to use the syntax print print in Python 3 will result in a SyntaxError because print is no longer recognized as a statement. To display the string representation of the print function in Python 3, you need to use print(print), which will output <built-in function print>.

Related questions

0 votes
1 answer
asked Mar 23, 2021 in Python by Rekha (2.2k points)
0 votes
4 answers
0 votes
0 answers

Browse Categories

...