小能豆

Python 3 print without parentheses

python

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"

阅读 86

收藏
2023-11-27

共1个答案

小能豆

In Python 3, print is a function and requires parentheses. However, if you really want to use the old syntax without parentheses, you can achieve it by adding the following import at the beginning of your script:

from __future__ import print_function

This import allows you to use the print function with the old Python 2 syntax in Python 3. Keep in mind that this is more of a workaround and might make your code less readable, so it’s generally recommended to adapt to the new syntax.

Here’s an example:

from __future__ import print_function

print("Hello stack over flowers")

With this import, you can still use print without parentheses in Python 3, but it’s important to note that this is a feature provided for transition purposes and might not be present in future Python versions.

2023-11-27