小能豆

multi-line literal works in python2 but not in python3

py

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?


阅读 95

收藏
2023-11-27

共1个答案

小能豆

In Python 3, the print statement is a function, and you need to use parentheses. Additionally, the triple single-quoted string is represented as ''', not ''''. Here’s the corrected version for Python 3:

print('''
Hello World
''')

Make sure to use parentheses with the print function and the correct string representation.

2023-11-27