多行文字可以在python2中使用,但不能在python3中使用 [英] multi-line literal works in python2 but not in python3

查看:167
本文介绍了多行文字可以在python2中使用,但不能在python3中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
使用Python 3打印时的语法错误

Possible Duplicate:
Syntax error on print with Python 3

我有以下代码:

print '''
Hello World
''''

它在Python 2上很好用,但在Python 3上不起作用.

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

我在做什么错了?

推荐答案

这不是多行问题,而是print问题.

It's not a problem of multi-line, but a problem of print.

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

print was replaced with a function print() in python 3, so that you have to call it as a function.

  • 在Python 3中不起作用:print 'hello'
  • 一个可以代替: print('hello')
  • won't work in Python 3: print 'hello'
  • the one works instead: print('hello')

对于您的情况,请尝试

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

这篇关于多行文字可以在python2中使用,但不能在python3中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆