Have a variable column length in printf

Cameron Pulsford cameron.pulsford at gmail.com
Sun May 31 12:33:58 EDT 2009


Thanks all, I think I'l stick with the '*' method because this is just a one
time thing for a __repr__ function and it seems easiest.

On Sun, May 31, 2009 at 6:32 AM, Terry Reedy <tjreedy at udel.edu> wrote:

> Gary Herron wrote:
>
>> Cameron Pulsford wrote:
>>
>>> Hello all, I'm trying to pretty print a list, so I am doing something
>>> like
>>>
>>> print '%3d' % integer
>>>
>>> only I would like that 3 to be a variable, instead of hardcoded. Is this
>>> possible, or are there any other ways to accomplish this? Thanks!
>>>
>>
>> Use a '*' instead of fixed width, and then supply the actual width as a
>> separate parameter:
>>
>>  >>> print '%*d' % (5,123)
>>  123
>>
>
> In the new string formating, fields can be nested withing fields.  For 3.1,
> with auto field numbering:
> >>> "{:{}d}".format(12,4)
> '  12'
>
> Otherwise, {0:{1}d}
>
> tjr
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090531/83b59b5c/attachment-0001.html>


More information about the Python-list mailing list