Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3 print without parenthesis

The print used to be a statement in Python 2, but now it became a function that requires parenthesis in Python 3.

Is there anyway to suppress these parenthesis in Python 3? Maybe by re-defining the print function?

So, instead of

print ("Hello stack over flowers") 

I could type:

print "Hello stack over flowers" 
like image 264
Laura Avatar asked Aug 20 '15 15:08

Laura


People also ask

Is there a way to print a single parentheses in Python?

So that's only a single extra character. If you still find typing a single pair of parentheses to be "unnecessarily time-consuming," you can do p = print and save a few characters that way. Because you can bind new references to functions but not to keywords, you can only do this print shortcut in Python 3.

Is parenthesis on print a function or a function?

In Python 3.x parenthesis on print is mandatory, essentially making it a function, but in 2.7 both will work with differing results.

What does'print'mean in Python?

Basically in Python before Python 3, print was a special statement that printed all the strings if got as arguments. So print "foo","bar"simply meant "print 'foo' followed by 'bar'".

How do I call functions without parentheses in IPython?

IPython lets you call functions without using parentheses if you start the line with a slash: Python 3.6.6 (default, Jun 28 2018, 05:43:53) Type 'copyright', 'credits' or 'license' for more information IPython 6.4.0 -- An enhanced Interactive Python.


1 Answers

Although you need a pair of parentheses to print in Python 3, you no longer need a space after print, because it's a function. So that's only a single extra character.

If you still find typing a single pair of parentheses to be "unnecessarily time-consuming," you can do p = print and save a few characters that way. Because you can bind new references to functions but not to keywords, you can only do this print shortcut in Python 3.

Python 2:

>>> p = print   File "<stdin>", line 1     p = print             ^ SyntaxError: invalid syntax 

Python 3:

>>> p = print >>> p('hello') hello 

It'll make your code less readable, but you'll save those few characters every time you print something.

like image 114
TigerhawkT3 Avatar answered Sep 28 '22 21:09

TigerhawkT3