[Tutor] Making table
Kent Johnson
kent37 at tds.net
Thu Mar 22 02:41:55 CET 2007
Alan Gauld wrote:
> others have discussed format strings.
> One final tip is to put the format string in a variable then use that
> in printing/writing the output
>
> eg:
>
> fmt = '%10.3f\t%10.3f\t%10.3f'
> print fmt % v1,v2,v3
>
> This has the advantage that you can build the format string
> dynamically by examining the data first - eg the maximum
> length of an entry.
You don't need to build the format string dynamically to get control of
the maximum width. A neat trick is that if you use '*' for the format
width, the width will be read from the parameter list:
In [30]: '%*.3f' % (5, 1.2)
Out[30]: '1.200'
In [31]: '%*.3f' % (10, 1.2)
Out[31]: ' 1.200'
Kent
More information about the Tutor
mailing list