[Tutor] Making table

Kent Johnson kent37 at tds.net
Mon Mar 19 20:59:29 CET 2007


Carroll, Barry wrote:
>> -----Original Message-----
>> Date: Mon, 19 Mar 2007 11:53:06 -0400
>> From: Kent Johnson <kent37 at tds.net>

>> Most string formatting conversions allow you to specify a width
>> directly. For example,
>> In [61]: value = 3.45678
>> In [63]: "%10.3f" % value
>> Out[63]: '     3.457'
>>
>> Kent
>>
> What if one wished to align the values in each column at the decimal
> point?  Is there a simple means to do this in Python, or would one need
> to write a new function?

If you specify the number of digits after the decimal point, that number 
of digits will always be given, so columns will line up:
In [86]: '%10.3f' % 1.2
Out[86]: '     1.200'
In [87]: '%10.3f' % 1.23456
Out[87]: '     1.235'

If you want something fancier I think you will have to do it yourself.

Kent


More information about the Tutor mailing list