【问题标题】:python 2.7 to python 3.2 string formatting errorpython 2.7 到 python 3.2 字符串格式错误
【发布时间】:2013-05-12 21:41:44
【问题描述】:

我有这段python代码:

args   =(12, 45, 7854) 
2 result = 123
3 answer = 456
4 print 'Test with data: ', args, ' result: ', result, ' answer: ', answer

现在当我用 python2.7.3 运行它时,我得到了这个:

Test with data:  (12, 45, 7854)  result:  123  answer:  456

但是当我使用 python3.2.3 我得到这个错误:

print 'Test with data: ', args, ' failed result: ', result, ' answer: ', answer
                       ^
SyntaxError: invalid syntax

我寻找了连接或格式化该字符串的不同方法,但使用 + 或 %s 会产生相同的错误。
任何帮助,在此先感谢。

【问题讨论】:

    标签: python string python-2.7 python-3.x string-concatenation


    【解决方案1】:

    print is a function 在 py3x 中:

    print ('Test with data: ', args, ' failed result: ', result, ' answer: ', answer)
    

    【讨论】:

      【解决方案2】:

      在 python 3+ 中,您需要输入 print( ),因为 2+ 中的打印 keyword 现在是 3+ 中的 function

      这里是Python Docs关于这个。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多