Python 3 print without parenthesis

124,865

Solution 1

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.

Solution 2

Using print without parentheses in Python 3 code is not a good idea. Nor is creating aliases, etc. If that's a deal breaker, use Python 2.

However, print without parentheses might be useful in the interactive shell. It's not really a matter of reducing the number of characters, but rather avoiding the need to press Shift twice every time you want to print something while you're debugging. 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. Type '?' for help.

In [1]: var = 'Hello world'

In [2]: /print var
Hello world

And if you turn on autocall, you won't even need to type the slash:

In [3]: %autocall
Automatic calling is: Smart

In [4]: print var
------> print(var)
Hello world

Solution 3

Use Autohotkey to make a macro. AHK is free and dead simple to install. www.autohotkey.com

You could assign the macro to, say, alt-p:

!p::send print(){Left}

That will make alt-p put out print() and move your cursor to inside the parens.

Or, even better, to directly solve your problem, you define an autoreplace and limit its scope to when the open file has the .py extension:

#IfWinActive .py            ;;; scope limiter
:b*:print ::print(){Left}   ;;; I forget what b* does. The rest should be clear 
#IfWinActive                ;;; remove the scope limitation

This is a guaranteed, painless, transparent solution.

Solution 4

No. That will always be a syntax error in Python 3. Consider using 2to3 to translate your code to Python 3

Solution 5

The AHK script is a great idea. Just for those interested I needed to change it a little bit to work for me:

SetTitleMatchMode,2         ;;; allows for a partial search 
#IfWinActive, .py           ;;; scope limiter to only python files
:b*:print ::print(){Left}   ;;; I forget what b* does
#IfWinActive                ;;; remove the scope limitation
Share:
124,865
Laura
Author by

Laura

Updated on October 22, 2021

Comments

  • Laura
    Laura over 2 years

    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"
    
  • Laura
    Laura almost 9 years
    I am using Python 3. I just want to avoid typing parenthesis all the time. shift + 9 ..... then again shift + 0 is exhaustive.
  • Laura
    Laura almost 9 years
    Thank you TigerhawkT3. Yes, this would save time, but like you said it'll make the code less readable.
  • holdenweb
    holdenweb almost 9 years
    Sorry that the function calls make the typing difficult for you. I'm a pretty crappy typist myself, so even though I don't have any physical disability I do what I can to minimize use of shifted keys.
  • Chandra Shekhar
    Chandra Shekhar almost 8 years
    @Tigerhawk This workaround is not compatible with IDE
  • TigerhawkT3
    TigerhawkT3 almost 8 years
    @ChandraShekhar - What IDE? What are you talking about? There's no reason for this to be affected by the choice of IDE.
  • Chandra Shekhar
    Chandra Shekhar almost 8 years
    @TigerhawkT3 I tried the same in Eclipse where it failed to work
  • TigerhawkT3
    TigerhawkT3 almost 8 years
    @ChandraShekhar - And is your Eclipse environment pointing to Python 2?
  • Chandra Shekhar
    Chandra Shekhar almost 8 years
    @TigerhawkT3 Its Python 3
  • TigerhawkT3
    TigerhawkT3 almost 8 years
    @ChandraShekhar - And in what way is it "not compatible"? What happens when you try it?
  • John Greene
    John Greene over 5 years
    Apparently, no one appreciate the simplicity of the UP arrow when using bash 'history recall'. It's simple. The same way with print, it should be unencumbered by useless debugging techniques such as a pair of parentheses.
  • holdenweb
    holdenweb over 5 years
    There are arguments for that, which were extensively rehearsed before the introduction of Python 3. While the extra parentheses may be inconvenient for us poor typists, the flexibility offered by the ability to redefine print was felt to be worth it, I believe.
  • Jack
    Jack over 5 years
    Here is a regex that works in NP++ (?<=print)( ')(.*)(') \('$2'\)
  • alchemy
    alchemy over 5 years
    Thanks @Jack, needed to escape the parentheses (just in replace?).. good to know. Also, I found a semi-related trick that Py3 recognizes having a space between print ('string'), so that the printable can more easily be highlighted and then use many editors' auto-close to add the parentheses and then quotes.
  • Arnaud
    Arnaud over 4 years
    Using python 2 as an alternative, as suggested by the post, is soon no more possible.