Em Dom, 2006-06-11 às 13:17 -0700, Saketh escreveu: > Stan Cook wrote: > > Can anyone tell me how to get the length of a number. I > > know len(string) will get the length of a string, but it > > doesn't like len(int). I seem to remember something like %s > > string. I tried to set a variable = to %s int, but that > > doesn't work. Is there a function I've forgotten about to > > convert an integer to a string? > > > > Regards > > > > Stan > > Use str(int). Then use len(). For example, len(str(12345)) will give > you 5.
$ python2.4 -mtimeit -s 'x=12345' 'len(str(x))' 1000000 loops, best of 3: 1.33 usec per loop $ python2.4 -mtimeit -s 'x=12345;from math import ceil,log' 'ceil(log(x, 10))' 1000000 loops, best of 3: 1.54 usec per loop $ python2.4 -mtimeit -s 'x=12345**123' 'len(str(x))' 1000 loops, best of 3: 209 usec per loop $ python2.4 -mtimeit -s 'x=12345**123;from math import ceil,log' 'ceil(log(x, 10))' 1000000 loops, best of 3: 1.55 usec per loop $ python2.4 -mtimeit -s 'x=12345**1234' 'len(str(x))' 100 loops, best of 3: 19.2 msec per loop $ python2.4 -mtimeit -s 'x=12345**1234;from math import ceil,log' 'ceil(log(x, 10))' 1000000 loops, best of 3: 1.53 usec per loop -- Felipe. -- http://mail.python.org/mailman/listinfo/python-list