[Tutor] Even More Converter!

Kepala Pening kepalapening at gmail.com
Sat Mar 22 17:43:08 CET 2008


sorry, I forgot that re search from the front of the string.

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


----- Original Message -----
From: bhaaluu <bhaaluu at gmail.com>
To: "Kepala Pening" <kepalapening at gmail.com>
Cc: tutor at python.org
Date: Sat, 22 Mar 2008 09:02:40 -0400
Subject: Re: [Tutor] Even More Converter!

> import re
> num = 12345678
> print ','.join(re.findall("\d{3}", str(num)))
> 
> output:
> 123,456
> 
> Where is the '78'?
> 
> It looks like that solution inserts comma's from left to right
> instead of from right to left.
> -- 
> b h a a l u u at g m a i l dot c o m
> "You assist an evil system most effectively by obeying its
> orders and decrees. An evil system never deserves such
> allegiance. Allegiance to it means partaking of the evil.
> A good person will resist an evil system with his or her
> whole soul." [Mahatma Gandhi]
> 
> On Sat, Mar 22, 2008 at 6:17 AM, Kepala Pening <kepalapening at gmail.com> 
wrote:
> >
> >  import re
> >
> >  num = 123456789
> >
> >  print ','.join(re.findall("\d{3}", str(num)))
> >
> >  output:
> >  123,456,789
> >
> >
> >
> >
> >  ----- Original Message -----
> >  From: wackedd at mac.com
> >  To: tutor at python.org
> >  Date: Fri, 21 Mar 2008 21:49:18 -0700
> >  Subject: [Tutor] Even More Converter!
> >
> >  It works perfectly, so I am sure my question will not be hard to answer.
> >  When Python gives me the answer to my conversion, is there a way to 
create it
> >  so every 3 numbers a comma is inserted?
> >  Such as: 1 mile is 5,280 feet. Instead of 1 mile is 5280 feet. Yes a 
simple
> >  thing, but something which I believe will make it look better.
> >  Also is there a way to make this so I don't have to go through every
> >  individual line of code and add *insert comma* or something to it, simply
> >  at the top like how the Unit Menu is placed only once there, and yet
> >  applies to the whole document. Thank you
> >  _______________________________________________
> >  Tutor maillist  -  Tutor at python.org
> >  http://mail.python.org/mailman/listinfo/tutor
> >
> 


More information about the Tutor mailing list