Search code examples
pythonpython-3.xpython-2.7multiline

multi-line literal works in python2 but not in python3


I have the following code:

print '''
Hello World
''''

It works well with Python 2 but does not work with Python 3:

Python 3.2.3 (default, Dec 10 2012, 06:30:54) 
[GCC 4.5.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print '''
... hello world
... '''
  File "<stdin>", line 3
    '''
      ^
SyntaxError: invalid syntax
>>> 

What am I doing wrong?


Solution

  • It's not a problem of multi-line, but a problem of print.

    print was replaced with a function print() in python 3, so that you have to call it as a function.

    • won't work in Python 3: print 'hello'
    • the one works instead: print('hello')

    For your case, try

    print('''
    Hello, 
    World
    ''')