print" hello"上的语法错误

时间:2014-07-31 18:01:08

标签: python python-3.x

我刚刚在一台新机器上安装了3.4,尝试运行我知道有效的东西并且失败了。然后我尝试将以下内容键入IDLE并失败:

>>> print 'hello'
SyntaxError: invalid syntax
>>> print hello
SyntaxError: invalid syntax
>>> print "hello"
SyntaxError: invalid syntax
>>> 

我很困惑为什么会失败。

2 个答案:

答案 0 :(得分:2)

根据Python3.0.1 documentation

The print statement has been replaced with a print() function, with keyword arguments to replace most of the special syntax of the old print statement (PEP 3105).

所以试试: print ("hello")

答案 1 :(得分:1)

我相信Python 3+你需要parens。试试这个:

print("Hello")

这是我的测试:

Python 3.3.2 (default, Sep 15 2013, 13:36:01)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print 'hello'
  File "<stdin>", line 1
    print 'hello'
                ^
SyntaxError: invalid syntax
>>>
>>>
>>> print("Hello")
Hello
>>>