Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is 'print' in Python?

Tags:

python

I understand what print does, but of what "type" is that language element? I think it's a function, but why does this fail?

>>> print print SyntaxError: invalid syntax 

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

>>> print print <function print at ...> 
like image 841
Mike Avatar asked Aug 11 '11 03:08

Mike


People also ask

What is called print in Python?

Python print() Function The print() function prints the specified message to the screen, or other standard output device. The message can be a string, or any other object, the object will be converted into a string before written to the screen.

What is the use of in print function in Python?

In Python, the print() function is used to get the output and debug the code. This function is used to display the specified message or value in the console. The message can be a string or any other object.

What is the use of print () method?

print(): print() method in Java is used to display a text on the console. This text is passed as the parameter to this method in the form of String. This method prints the text on the console and the cursor remains at the end of the text at the console.

What is print in programming?

In many programming languages, print is a function that sends text, variables, or another object to the screen. If you're more familiar with the programming language Scratch, print is equivalent to say. Below is an example of "Hello World!" getting print to the screen in Perl.


1 Answers

In 2.7 and down, print is a statement. In python 3, print is a function. To use the print function in Python 2.6 or 2.7, you can do

>>> from __future__ import print_function >>> print(print) <built-in function print> 

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

like image 136
wim Avatar answered Sep 22 '22 19:09

wim