Python: a few comments about the print command
The tutor seeks to clarify some ideas about Python’s print command.
I refreshed Python on one of my computers yesterday; I uninstalled Python 2.7, then installed Python 3.4 instead. To do so, I went to python.org. I was using a Windows 7 computer at the time; the site suggested a download for me, so I just followed its lead. Later I tried a test script. The whole process took inside of 10 minutes. Everything works flawlessly, so far as I know.
Of course, there are differences between Python 2.x and 3.x. A key one that might affect people still running Python 2.x concerns the print command.
In Python 2.x, print doesn’t require brackets. Therefore,
print “Hello!” #2.x
works perfectly. However, in Python 3.x, brackets are required:
print(“Hello!”) #3.x
In 2.x, print always attaches a newline. Therefore,
print “Hello!”
print “Ciao!”
will give the output
Hello!
Ciao!
In 3.x, you can stop the newline’s being added with end=” “:
print(“Hello!”, end=” “)
print(“Ciao!”)
gives the output
Hello! Ciao!
To my knowledge, the end=” “ trick doesn’t work in Python 2.x.
HTH:)
Source:
Donaldson, Toby. Python, 3rd Ed. Peachpit Press, 2014.
Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.
Leave a Reply
You must be logged in to post a comment.