Syntax error on print with Python 3 [duplicate]

2018-12-30 23:18发布

Why do I receive a syntax error when printing a string in Python 3?

>>> print "hello World"
  File "<stdin>", line 1
    print "hello World"
                      ^
SyntaxError: invalid syntax

9条回答
美炸的是我
2楼-- · 2018-12-30 23:25

It looks like you're using Python 3. In Python 3, print has been changed to a method instead of a statement. Try this:

print("hello World")
查看更多
不流泪的眼
3楼-- · 2018-12-30 23:25

You have to use brackets with print:

print("Hello, World!")
查看更多
闭嘴吧你
4楼-- · 2018-12-30 23:30

It looks like you're using Python 3.0, in which print has turned into a callable function rather than a statement.

print('Hello world!')
查看更多
怪性笑人.
5楼-- · 2018-12-30 23:32

In Python 3, you must do print('some code'). This is because in Python 3 it has become a function. If you must, you can use your Python 2 code and convert it to Python 3 code using 2to3 - it is a great built-in program which comes with Python. For more, see Python 2to3 - Convert your Python 2 to Python 3 automatically!.

查看更多
心情的温度
6楼-- · 2018-12-30 23:34

In Python 3, it's print("something") , not print "something".

查看更多
余生无你
7楼-- · 2018-12-30 23:37

In Python 3.0, print is a regular function that requires ():

print("Hello world")
查看更多
登录 后发表回答