Print/string formatting

Below is a quick explanation of the so-called "old way" of formatting strings in Python. 
Here is a longer tutorial on the old and new ways: http://www.python-course.eu/python3_formatted_output.php

You can format output for printing (or any other purpose, like writing to files), using the % operator in Python.  Here's an interactive explanation of some of the ways to do this. But first, some rules:

Formatting consists of a formatting string following the character % followed by the variables (or literal values) you want to insert into the formatted string...

> pi=3.141592653
> last="Potter"
> first='Harry'
> age=15
> print first,pi
Harry 3.141592653

> #### Here's our first use of formatting:
> print "Harry's last name is %s", last
Harry's last name is Potter

> #### If there's more than one variable to insert into the formatting string, enclose the variables inside parentheses:
> print 'Official name: %s, %s' % (last,first)
Official name: Potter, Harry

> #### Use %f for floating point numbers (let Python choose the number of decimal places
> print '%f' % pi
3.141593

> #### Below, we want 6 spaces to hold the number with 3 of them allocated to decimal places
> print '%6.3f' % pi
3.142
> print '%10.3f' % pi
   3.142
> print '%20.5f is pi, %s' % (pi,first)
           3.14159 is pi, Harry

> #### Use %d for integers
> Q='The age of %s is %d' % (first,age)
> print Q
The age of Harry is 15