multi-line literal works in python2 but not in pyt

2019-08-18 03:46发布

问题:

Possible Duplicate:
Syntax error on print with Python 3

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?

回答1:

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