Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

leading zeros in python [duplicate]

Python seems to be able to accept leading zeros for any number except 08 or 09. For instance,

a = 04

works in the interpreter but

a = 08

returns

SyntaxError: invalid token

I'm using python 2.7.3 on OSX, and others have been able to duplicate the error. What gives?

like image 930
user2171504 Avatar asked Jul 04 '26 08:07

user2171504


1 Answers

Numbers with a leading zero in them are interpreted as octal, where the digits 8 and 9 don't exist.

It's worse in Python 3, leading zeros are a syntax error no matter which digits you use. See What’s New In Python 3.0 under "New octal literals". Also PEP 3127.

like image 73
Mark Ransom Avatar answered Jul 06 '26 22:07

Mark Ransom