By Scott

2009-05-05 21:19:19 8 Comments

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


@user4909217 2015-05-18 12:04:40

You have to use brackets with print:

print("Hello, World!")

@lmiguelvargasf 2015-04-30 05:39:37

In Python 2.X print is a keyword, while in Python 3.X print becomes a function, so the correct way to do it is print(something).

You can get the list of keywords for each version by executing the following:

>>> import keyword
>>> keyword.kwlist

@brianz 2009-05-05 21:21:24

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!')

@Unknown 2009-05-05 21:21:18

In Python 3, print became a function. This means that you need to include parenthesis now like mentioned below:

print("Hello World")

@AtPython 2015-08-13 09:35:48

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!.

@Paweł Polewicz 2009-05-05 21:22:20

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

@ChillarAnand 2014-10-27 12:35:26

Because in Python 3, print statement has been replaced with a print() function, with keyword arguments to replace most of the special syntax of the old print statement. So you have to write it as

print("Hello World")

But if you write this in a programme and some one using Python 2.x tries to run, they will get an error. To avoid this, it is a good practice to import print function

from __future__ import print_function

Now you code works on both 2.x & 3.x

Check out below examples also to get familiar with print() function.

Old: print "The answer is", 2*2
New: print("The answer is", 2*2)

Old: print x,           # Trailing comma suppresses newline
New: print(x, end=" ")  # Appends a space instead of a newline

Old: print              # Prints a newline
New: print()            # You must call the function!

Old: print >>sys.stderr, "fatal error"
New: print("fatal error", file=sys.stderr)

Old: print (x, y)       # prints repr((x, y))
New: print((x, y))      # Not the same as print(x, y)!

Source: What’s New In Python 3.0?

@TwentyMiles 2009-05-05 21:22:09

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")

@Ayman Hourieh 2009-05-05 21:21:44

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

print("Hello world")

Related Questions

Sponsored Content

26 Answered Questions

17 Answered Questions

[SOLVED] Python string formatting: % vs. .format

18 Answered Questions

[SOLVED] Does Python have a string 'contains' substring method?

55 Answered Questions

[SOLVED] Calling an external command in Python

8 Answered Questions

[SOLVED] Proper way to declare custom exceptions in modern Python?

16 Answered Questions

[SOLVED] Convert bytes to a string?

15 Answered Questions

[SOLVED] What are metaclasses in Python?

9 Answered Questions

[SOLVED] Converting integer to string in Python?

  • 2009-06-07 10:22:38
  • Hick
  • 2836371 View
  • 1131 Score
  • 9 Answer
  • Tags:   python

10 Answered Questions

191 Answered Questions

[SOLVED] Hidden features of Python

Sponsored Content