multi-line literal works in python2 but not in python3 [duplicate]

不问归期 提交于 2019-12-25 09:38:41

问题


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


来源:https://stackoverflow.com/questions/14184948/multi-line-literal-works-in-python2-but-not-in-python3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!