Python - print in Python 2.X and 3.X

Introduction

The following table lists the print statement's forms in Python 2.X and gives their Python 3.X print function equivalents for reference.

Python 2.X statement
Python 3.X equivalent
Interpretation
print x, y
print(x, y)
Print objects' textual forms to sys.stdout
print x, y,
print(x, y, end='')
Same, but don't add end-of-line at end of text
print >> afile, x, y

print(x, y, file=afile)

Send text to afile.write, not to
sys.stdout.write

Example

c:\python27\python 
>>> x = 'a' 
>>> y = 'b' 
>>> print x, y 
a b 
>>> print x, y,; print x, y 
a b a b 

print x + y 
print '%s...%s' % (x, y) 

Related Topic