selinap.com

To leave furtively and stealthily.

Home » Python

Python: Put comma(s) to numbers

April 15th, 2008

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

1
2
3
4
5
6
7
8
9
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

Related Articles:

Popular Articles:

Spread/Promote this article.

Digg | Del.icio.us | Stumble | Y! MyWeb | Y! Buzz | Fave It! | Reddit

Subscribe for free.

 Subscribe to Selinap.com feed right now!

 Get Updates by Email

Tags: , ,

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="">


© Copyright 2008 - 2009 selinap.com
Entries (RSS) and Comments (RSS).