examples/python/commafy.py

from __future__ import print_function

small = 42
big = 1234567

print(small)      # 42
print(big)        # 1234567

print("{:,}".format(small)) # 42
print("{:,}".format(big))   # 1,234,567

We have imported the print_function from the __future__ so this code will work on both Python 2 and Python 3.

The key here is the {:,} placeholder.