Why is the bracket printed in Python 2.7?

In Python 2.7, both actions will do the same.

print("Hello, World!") # Prints "Hello, World!" print "Hello, World!" # Prints "Hello, World!" 

However, the following will not be

 print("Hello,", "World!") # Prints the tuple: ("Hello,", "World!") print "Hello,", "World!" # Prints the words "Hello, World!" 

Python 3.x is required in brackets with print , which makes it a function, but in 2.7 both will work with different results. What else should I know about print in Python 2.7?

+90
python printing
May 31 '11 at 4:18
source share
4 answers

In Python 2.x, print actually has a special operator, not a * function.

That is why it cannot be used like: lambda x: print x

Note that (expr) does not create a Tuple (this leads to expr ), but does. This probably leads to confusion between print (x) and print (x, y) in Python 2.7

 (1) # 1 -- no tuple Mister! (1,) # (1) (1,2) # (1,2) 1,2 # 1 2 -- no tuple and no parenthesis :) [See below for print caveat.] 

However, since print is a special syntax operator / grammar construct in Python 2.x, without a bracket, it processes , special way - and does not create a Tuple. This special handling of the print statement allows it to act differently if the finite exists or not.

Happy coding.




* This print behavior in Python 2 can be changed to the behavior of Python 3:

 from __future__ import print_function 
+96
May 31 '11 at 4:25
source share

Here we have an interesting side effect when it comes to UTF-8.

 >> greek = dict( dog="σκύλος", cat="γάτα" ) >> print greek['dog'], greek['cat'] σκύλος γάτα >> print (greek['dog'], greek['cat']) ('\xcf\x83\xce\xba\xcf\x8d\xce\xbb\xce\xbf\xcf\x82', '\xce\xb3\xce\xac\xcf\x84\xce\xb1') 

The last print is a tuple with hexadecimal byte values.

+6
Aug 15 '12 at 17:05
source share

Basically in Python before Python 3, printing was a special expression in which all lines were printed if they were received as arguments. So print "foo","bar" simply meant "print 'foo" followed by "bar". The problem was that it was tempting to act as if printing was a function and the Python grammar is ambiguous, since (a,b) is a tuple containing a and b , but foo(a,b) is a function call of two arguments.

So they made an incompatible change for 3 to make the programs less ambiguous and more regular.

(Actually, I think 2.7 behaves like 2.6, but I'm not sure.)

+2
May 31 '11 at 4:28 a.m.
source share

All this is very simple and has nothing to do with backward or backward compatibility.

A general view for the print statement in all versions of Python prior to version 3:

 print expr1, expr2, ... exprn 

(Each expression, in turn, is evaluated, converted to a string, and displayed with a space in between.)

But remember that putting parentheses around an expression remains the same expression.

So you can also write this as:

 print (expr1), (expr2), ... (expr3) 

This has nothing to do with calling a function.

+2
May 31 '11 at 5:33 a.m.
source share



All Articles