The print statement and integer division
Two Python 2 behaviours that change results without a warning: print as a statement, and division that floors between integers.
print is a statement, not a function
import sys
print "hello" # no parentheses required
print "value:", 42 # a trailing comma means "no newline"
print "still the same line",
print # a bare print emits just the newline
print >>sys.stderr, "warning: writing to a file object"
print >>open("out.txt", "w"), "redirected"
print "count = " + 3 # TypeError: cannot concatenate 'str' and 'int'
print "count =", 3 # this works, because print applies str() to each item- The trailing comma does not separate items — it suppresses the newline and adds a space before the next output.
print >>f, xis the redirect syntax: any object with awritemethod will do, includingsys.stderr.- Parentheses are just grouping here, so
print("a", "b")prints the tuple('a', 'b')— a two-item tuple, not two words. - Adding
from __future__ import print_functionas the first line of a file makes the identical syntax behave exactly as in Python 3. - A bare
printis also valid inside a function, where Python 3 would needprint().
Division between integers floors
print 7 / 2 # 3 int / int floors, it does not truncate
print 7.0 / 2 # 3.5 one float operand promotes the result
print -7 / 2 # -4 floors toward negative infinity, so NOT -3
print 7 % -2 # -1 the result takes the sign of the divisor
print 7 // 2 # 3 the explicit floor operator exists in Python 2 too
from __future__ import division # must be the first statement in the file
print 7 / 2 # 3.5 true division, matching Python 3
print 7 // 2 # 3 integer division is now explicit| Expression, no future import | Result | Why |
|---|---|---|
| 7 / 2 | 3 | int / int is floor division |
| 7 // 2 | 3 | Explicit floor division, same answer |
| 7.0 / 2 | 3.5 | A float operand promotes the result |
| -7 / 2 | -4 | Floors downward; it does not truncate toward zero |
| -7 // 2 | -4 | The same flooring rule |
| 7 % -2 | -1 | The remainder follows the sign of the divisor |
| 10 ** 20 / 3 | A long | Integers promote to long automatically on overflow |
| float(7) / 2 | 3.5 | The portable way to force true division today |
⚠️
This is the single most damaging porting bug in Python 2 code, because it fails silently.
total / count computes an average in Python 3 and a floored quotient in Python 2, and every test written with values that divide exactly will still pass. Put from __future__ import division at the top of every legacy file you touch, then review each / and change it to // where you really did mean integer division.FAQ
Why does print add a space between items?
Because that is what the statement does: it applies
str() to each argument and joins them with a single space, then writes a newline unless the line ends with a comma. There is no way to change the separator without switching to print_function or building the string yourself.Can I use parentheses with print to be future-proof?
Only with the future import. Without it,
print("a", "b") is a tuple expression and prints ('a', 'b'); with a single argument it looks correct by accident, which is exactly why the trap survives unnoticed.Related
Migrating to Python 3 Python: getting started
Last refreshed 2026-09-18.