print formatting

Steve Holden steve at holdenweb.com
Sat Mar 13 08:31:32 EST 2010


vsoler wrote:
> Hello,
> 
> My script contains a print statement:
> 
>          print '%40s     %15d' % (k, m)
> 
> However,
> 
>          1- the string is right adjusted, and I would like it left
> adjusted
>          2- the number is a decimal number, and I would like it with
> the thousands separator and 2 decimals
> 
> If possible, the thousands separator and the decimal separator should
> use my local settings.
> 
> Is there any way to achieve this?

Left-alignment is achieved by using a negative width.

You can use the locale module to generate thousands-separated numeric
string representations:

>>> from locale import *
>>> setlocale(LC_ALL, '') # locale is otherwise 'C'
'en_US.UTF-8'
>>> locale.format("%12.3f", 123456.789, grouping=False)
'  123456.789'
>>> locale.format("%12.3f", 123456.789, grouping=True)
' 123,456.789'

regards
 Steve
-- 
Steve Holden           +1 571 484 6266   +1 800 494 3119
See PyCon Talks from Atlanta 2010  http://pycon.blip.tv/
Holden Web LLC                 http://www.holdenweb.com/
UPCOMING EVENTS:        http://holdenweb.eventbrite.com/




More information about the Python-list mailing list