String formatting with fixed width

Alexander Eisenhuth newsuser at stacom-software.de
Fri Mar 16 10:28:42 EDT 2007


Thanks for your fast reply. I'm fine with your "%7.03f" solution. (negatives are 
  not significant)

Alexander Eisenhuth schrieb:
> Hello alltogether,
> 
> is it possible to format stings with fixed width of let's say 7 
> character. T need a floating point with 3 chars before dot, padded with 
> ' ' and 3 chars after dot, padded with '0'.
> 
> Followingh is my approach
>  >>> f = 21.1
>  >>> s = "%.03f" % f
>  >>> s
> '21.100'
> 
> But there are missing ' '. How can I get that? (For bigger numbers than 
> 999 they might be cut: 1021 -> 021)
> 
> Thanks,
> Alexander



More information about the Python-list mailing list