Add commas into number string [duplicate]

In Python 2.7 and 3.x, you can use the format syntax :,

>>> total_amount = 10000
>>> print("{:,}".format(total_amount))
10,000
>>> print("Total cost is: ${:,.2f}".format(total_amount))
Total cost is: $10,000.00

This is documented in PEP 378 — Format Specifier for Thousands Separator and has an example in the Official Docs “Using the comma as a thousands separator”

Leave a Comment