【问题标题】:multi-line literal works in python2 but not in python3 [duplicate]多行文字在python2中有效,但在python3中无效[重复]
【发布时间】:2012-12-20 12:47:23
【问题描述】:

可能重复:
Syntax error on print with Python 3

我有以下代码:

print '''
Hello World
''''

它适用于 Python 2,但不适用于 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
>>> 

我做错了什么?

【问题讨论】:

标签: python python-3.x python-2.7 multiline


【解决方案1】:

不是多行的问题,而是print的问题。

print 在 python 3 中被替换为函数print(),所以你必须将其作为函数调用。

  • 在 Python 3 中不起作用:print 'hello'
  • 一个可以代替: print('hello')

你的情况,试试

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

【讨论】:

    猜你喜欢
    • 2019-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-06
    • 2012-01-25
    • 2014-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多