Veterans to software development will no doubt be ready to take a look at the famous “Hello World!” program, typically the first program that a programmer experiences when exposed to a new language. There is no exception here.
>>> print "Hello World" Hello World
The print statement is used to display output to the screen. Those of you who are familiar with C are aware that the printf() function produces screen output. Many shell script languages use the echo command for program output.
Usually when you want to see the contents of a variable, you use the print statement in your code. However, from within the interactive interpreter, you can use the print statement to give you the string representation of a variable, or just dump the variable raw—this is accomplished by simply giving the name of the variable.
In the following example, we assign a string variable, then use print to display its contents. Following that, we issue just the variable name.
>>> String1 = "Hello World" >>> print String1 Hello World >>> String1 'Hello World' >>>
Notice how just giving only the name reveals quotation marks around the string. The reason for this is to allow objects other than strings to be displayed in the same manner as this string —being able to display a printable string representation of any object, not just strings. The quotes are there to indicate that the object whose value you just dumped to the display is a string.
The print statement, paired with the string format operator ( % ), behaves even more like C’s printf() function, as follows :
>>> print "%s is number %d" %("python",1) python is number 1 >>>