break up integer

How to break up an integer like this: 1234567 into a more humanly readable form like this: 1 234 567, when printing?


"{:_}".format(n).replace("_", ' ')

Or with underscores or commas


"{:_}".format(n)


"{:,}".format(n)

Could you give me an example? If I have a variable: n = 1234567, how exactly could I print it broken up?

I am sure there is a fancier way, but strings are like arrays, so you can loop through the characters as well(backwards in this case):



n = 1234567

def format_number(number):
    string = ''
    count = 0
    for every_char in str(number)[::-1]:
        count = count + 1
        string = string + every_char
        if count%3 == 0:
            string = string + ' '
    return string[::-1]
    
print(format_number(n))


Thanks for both of you.

Once Blender moves to Python 3.6, you will be able to do this:

 >>> format(1234567, ",")
'1,234,567'

or this:

 >>> format(1234567, "_")
'1_234_567'