版本python2和版本3.X的一个区别之一
print函数
虽然print语法是Python 3中一个很小的改动,且应该已经广为人知,但依然值得提一下:Python 2中的print语句被Python 3中的print()函数取代,这意味着在Python 3中必须用括号将需要输出的对象括起来。
在Python 2中使用额外的括号也是可以的。但反过来在Python 3中想以Python2的形式不带括号调用print函数时,会触发SyntaxError。
Python 2
|
1
2
3
4
|
print 'Python', python_version()print 'Hello, World!'print('Hello, World!')print "text", ; print 'print more text on the same line' |
|
1
2
3
4
|
Python 2.7.6Hello, World!Hello, World!text print more text on the same line |
Python 3
|
1
2
3
4
5
|
print('Python', python_version())print('Hello, World!') print("some text,", end="") print(' print more text on the same line') |
|
1
2
3
|
Python 3.4.1Hello, World!some text, print more text on the same line |
|
1
|
print 'Hello, World!' |
File "<ipython-input-3-139a7c5835bd>", line 1print 'Hello, World!'^SyntaxError: invalid syntax
浙公网安备 33010602011771号