Formatting question : printing numbers with thousands separators ?

Gerson Kurz gerson.kurz at t-online.de
Fri Aug 31 14:49:06 CEST 2001


On 31 Aug 2001 11:33:52 GMT, Fred Pacquier <fredp at mygale.org.nospam>
wrote:

>how do I print 246814345 as 246,814,345 (or 246 814 345) ?

def f(x):
    s,i = list(str(x)),3
    while i < len(s):
        s.insert(len(s)-i,',')
        i += 4
    return ''.join(s)

print f(24)
print f(24312)
print f(2431229082L)

will output
24
24,312
2,431,229,082




More information about the Python-list mailing list