The Personal Web Pages of Chris X. Edwards

XED's Python Lessons for Beginners

--------------------------

Lesson Five - Charming Prints

In previous lessons I have used the `print` command in examples. In this lesson I'll take a better look at the print command and some of the useful things it can do. The print command is really quite simple and you probably have already figured out that when a running Python program comes to a print statement, output is generated. That's really the extent of the print command itself. What makes it more interesting is what you can print. The print command is the simple way to take advantage of Python's fancy string formatting tricks. This is a good topic for beginners to learn since it quickly provides opportunities for creating simple but useful programs.

Strings can be added in an expected way:
print 'Pythons ' + 'eat ' + ' mice'
    

If that wasn't what you were expecting, this works the same:
print 'Pythons ' 'eat ' ' mice'
    

More idiosyncratic is Python's ability to "multiply" strings:
print 'This keeps happening ' + 'over and '*4 + 'over.'
This keeps happening over and over and over and over and over.
    

If you want multiline output, you can use the special new line code:
print 'Line one\nLine two\nLine three"
    
There are other special codes but `\n` is probably the most important.

In C (and Perl) text can be formatted for nice output with the very fancy command, printf. Python has printf's capabilities in a more elegant syntax. If you don't know this command, you're not missing much. It's probably better to learn about it after knowing how Python handles text substitution and formatting. The basic format of the substitution technique in Python is like this:
>>> euphemism= 'enjoy'
>>> print "Pythons %s mice." % euphemism
Pythons enjoy mice.

>>> print '%s is %.2f meters tall and %d kg' % ('Chris', 1.77, 70)
Chris is 1.77 meters tall and 70 kg

>>> print ("%10.3f\n"*7) % (1e-3, 1e-2, 1e-1, 1e0, 1e1, 1e2, 1e3)
     0.001
     0.010
     0.100
     1.000
    10.000
   100.000
  1000.000
    

Just to clarify where those letters after the % come from, s is for string. For numerical values d and i both stand for "decimal integer". This doesn't mean it has a decimal point, but rather it's decimal as opposed to hexadecimal or some other base. A decimal integer can be thought of as a whole number without a decimal point. For numbers with decimal points use f which stands for floating point number.

Text can be specified over several lines using triple quote (single or double). Here is an example of where that might be useful:
mailing_label= """Ship To:
%(NAME)s
%(ADDRESS)s
%(CITY)s, %(STATE)s %(ZIP)i"""
    

When using triple quotes, indentation is taken literally, not as part of Python's code structure. This example also illustrates a powerful kind of text substitution where the fields are defined in Python dictionaries. When a string contains something in the form`%(key_name)s`, then a dictionary can be used to fill in the values like this:
address= {'NAME':'Google', 'ADDRESS':'1600 Amphitheatre Parkway',
          'CITY':'Mountain View', 'STATE':'CA', 'ZIP':94043}
    
Then you can use the '%' operator to fill in the form:
print mailing_label % address
    
Notice in the example here that each variable in the text ends with an 's' except the zip which ends in an 'i'. This stands for "string" and "integer" respectively. Also notice with the zip that it is not quoted in the dict because it is actually a number. This may or may not be a smart way to organize things depending on your task, but it shows how this kind of substitution works.

Finally, note that if you're putting these examples into the interactive interpreter, you do not need to say "print" since the default behavior of the interactive mode is to show any object you enter. So in interactive mode:
>>> print "Pythons %s mice." % euphemism
    
does the same thing as:
>>> "Pythons %s mice." % euphemism
    
The '%' operator is something that operates on strings no matter what is happening to them and not related to the print command. Of course the print command is necessary, however, when you want some output to escape from a running Python program.

--------------------------
Previous LWM Home Python Lessons Index Next
This page was created with only free, open-source, publicly licensed software.
This page was designed to be viewed with any browser on any system.
Chris X. Edwards ~ June 2008