0

What are options for keeping as close to the python2 way of printing.

>>> x1='hi'
>>> x2='there'
>>> print "Val1=%s Val2=%s" %(x1,x2)
  File "<stdin>", line 1
    print "%s" %x
             ^
SyntaxError: invalid syntax

It would not be necessary to mention : "lose your 'old' way of thinking and use the {} in python3." I am aware of the "blessed" python3 syntax, and given it is not in my estimation preferable, would like to see what other options exist.

thanks.

4

2 回答 2

7

这在 python 3 中仍然是有效的语法,但print 不再是 statement。它是一个函数,所以你必须放括号:

print("%s" % x) 
于 2013-07-23T22:16:00.670 回答
3

没有任何。如果您想要完美的兼容性,请避免print/完全。print()

sys.stdout.write('%s\n' % (x,))
于 2013-07-23T22:16:36.507 回答