多行文字在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之间发生了哪些变化,因为还有其他不太容易偶然发现的重大变化。