[Tutor] Formatting output into columns

Kent Johnson kent37 at tds.net
Fri Aug 31 00:21:47 CEST 2007


Alan Gauld wrote:
> "Scott Oertel" <freebsd at scottevil.com> wrote
>> Do you have any good documentation that could shed some more light 
>> on
>> exactly how to use format strings in such a way?
> 
> The docs contain the basic documentation

http://docs.python.org/lib/typesseq-strings.html

> # there's a slightly better way to do this which I can't recall right 
> now!
> # %% -> literal %,
> # %s = insert string
> # so %%%ss -> %Xs where X is the inserted data
> fmtStr = "%%%ss\t%%%ss%%%ss" % (max_width, max_width, max_width)
> 
> print fmtStr % tuple(data)

I think you are looking for * as a width specifier which takes the width 
from the argument list though it might be a bit awkward in this case 
where the length of the argument list varies.

Kent


More information about the Tutor mailing list