How to print an integer with commas; E.g., 3,056,789

Padraig Brady Padraig at Linux.ie
Wed Oct 16 08:02:21 EDT 2002


Richard Bow wrote:
> Given any integer n, how can I convert str(n) to a string with commas in 
> the appropriate places? For example, if n is 3056789, I'd like to convert 
> str(3056789) to "3,056,789", for better readability of output.

Yes wouldn't it be nice to have:

 >>> i=3056789
 >>> print "%h %.2m %.2M %.2k %.2K" % i, i, i, i, i
3,056,789 3.06M 2.92Mi 3056.79K 2985.15Ki

Pádraig.




More information about the Python-list mailing list