Octal number problem

Gary Duncan gmduncan at netspace.net.au
Wed Feb 19 21:19:32 EST 2003


Python has the quaint behaviour of interpreting
a numeric string with a leading zero as octal, a bit like C.

How does one disable this behaviour, ie get it to
treat such strings as a "normal" decimal number ?

I've had a quick search via Google for an answer and nothing
lept out at me. Lutz and Aschers "Learning Python" book had the
'wink-wink/nudge-nudge' comment "Python interprets them  [numbers with
leading 0s]  as octal constants, which usually don't work as you'd expect!'

(Was that from "Life of Brian" ? the exclamation mark was theirs, btw ;)

(In an item a few days ago, I complained of the unhelpful
   diagnostic one gets from Python when one inputs say "0999";
   ie "Syntax Error: invalid token". But thats a side issue)


A bit of background:-

	I have a list of 5-digit decimal numbers, which was constructed
with leading zeroes where necessary so that the list is sorted by magnitude
using Unix 'sort' (otherwise '00200' would appear before '1').


- Gary





More information about the Python-list mailing list