format a number for output

Tim Williams listserver at tdw.net
Mon Aug 7 14:22:25 EDT 2006


On 7 Aug 2006 07:55:11 -0700, abcd <codecraig at gmail.com> wrote:
> if i have a number, say the size of a file, is there an easy way to
> output it so that it includes commas?
>
> for example:
>
> 1890284
>
> would be:
>
> 1,890,284
>

I was bored !!

>>> a = 1890284
>>> ','.join([str(a)[::-1][x:x+3] for x in range(len(str(a)))[::3]])[::-1]
'1,890,284'

Ugly !

:)



More information about the Python-list mailing list