【问题标题】:Why is printing this formatted string causing a syntax error? [duplicate]为什么打印此格式化字符串会导致语法错误? [复制]
【发布时间】:2015-12-24 04:13:37
【问题描述】:

我几天来一直在尝试使用 Python 的新 print 格式。为了研究这个问题,我已经剪切并粘贴了几个书籍示例。

print '{0} and {1}'.format('spam', 'eggs')

即使这个例子也有效果:

print '{0} and {1}'.format('spam', 'eggs')
                      ^
SyntaxError: invalid syntax

发生了什么事?

【问题讨论】:

标签: python printing format output


【解决方案1】:

试试:

print('{0} and {1}'.format('spam', 'eggs'))

在 Python 3 中,print 不是语句,必须调用为 print()

输出:

spam and eggs

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-11
    • 2015-08-02
    • 2012-10-04
    相关资源
    最近更新 更多