Next: Optional Function Arguments Up: The Formatting Operator Previous: Basic Usage

Referencing Variables By Name

If you have a really long format string that you don't want to split up, it would be nice if you could reference the variables to be formatted by name instead of by position. This can be done by using an extension of C formats using the form %(name)format, e.g.


        >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
        >>> print 'Jack: %(Jack)d; Sjoerd: %(Sjoerd)d; Dcab: %(Dcab)d' % table
        Jack: 4098; Sjoerd: 4127; Dcab: 8637678
        >>>

This is particularly useful in combination with the new built-in vars() function, which returns a dictionary containing all local variables.


guido@cwi.nl