Have a variable column length in printf

Gary Herron gherron at islandtraining.com
Sun May 31 09:21:37 CEST 2009


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

Gary Herron





More information about the Python-list mailing list