Python: Put comma(s) to numbers

This script will put commas between each group of three digits number. For example, 1234567 will become 1,234,567.

import re

putComma = lambda x: (",".join(re.findall("\d{1,3}", \
    str(x)[::-1])))[::-1]

print putComma(1234567) # 1,234,567
print putComma(12345678) # 12,345,678
print putComma(123456789) # 123,456,789
print putComma(1212) # 1,212
If you are new here, you might want to subscribe to the RSS feed or newsletter.

Enter your email address:

Related

This entry was posted in Python and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>