Underscores in Python numbers

Dan Bishop danb_83 at yahoo.com
Sun Nov 20 03:51:11 EST 2005


Roy Smith wrote:
> Steven D'Aprano <steve at REMOVETHIScyber.com.au> wrote:
> > That's a tad unfair. Dealing with numeric literals with lots of digits is
> > a real (if not earth-shattering) human interface problem: it is hard for
> > people to parse long numeric strings.
>
> There are plenty of ways to make numeric literals easier to read without
> resorting to built-in language support.  One way is:
>
> sixTrillion = 6 * 1000 * 1000 * 1000 * 1000
>
> Or, a more general solution might be to write a little factory function
> which took a string, stripped out the underscores (or spaces, or commas, or
> whatever bit of punctuation turned you on), and then converted the
> remaining digit string to an integer.  You could then write:
>
> creditCardNumber = myInt ("1234 5678 9012 3456 789")

Or alternatively, you could write:

creditCardNumber = int('1234''5678''9012''3456''789')




More information about the Python-list mailing list