You can also use the write method of sys.stdout . The string module has some useful formatting things. The string should be more or less a C-style printf format string. The RH operand can be a sequence, whose values will be fed in order to the format directives in the string. Python has automatic memory management, so you needn't bother freeing things when you're finished with them.
Functions are first-class objects.
You can nest function definitions, but NB that according to Python's scoping rules the inner functions can't see the outer functions' variables. You can get some way towards fixing this using default arguments.