Underscores in Python numbers

Roy Smith roy at panix.com
Sun Nov 20 04:13:16 CET 2005

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")

Perhaps not as convenient as having it built into the language, but 
workable in those cases which justify the effort.

More information about the Python-list mailing list