Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
多行文字在python2中有效,但在python3中无效_Python_Python 3.x_Python 2.7_Multiline - Fatal编程技术网

多行文字在python2中有效,但在python3中无效

多行文字在python2中有效,但在python3中无效,python,python-3.x,python-2.7,multiline,Python,Python 3.x,Python 2.7,Multiline,可能重复: 我有以下代码: 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

可能重复:

我有以下代码:

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 3.2.3(默认,2012年12月10日06:30:54)
[GCC 4.5.4]关于linux2
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>打印“”'
... 你好,世界
... '''
文件“”,第3行
'''
^
SyntaxError:无效语法
>>> 

我做错了什么?

这不是多行的问题,而是打印的问题

在python 3中,
print
被替换为函数
print()
,因此必须将其作为函数调用

  • 在Python 3中不起作用:
    打印“hello”
  • 一个有效的方法是:
    print('hello')
对于你的情况,试试看

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

在python3中使用
print()
作为函数。您应该通读文档,了解在2和3之间发生了哪些变化,因为还有其他不太容易偶然发现的重大变化。