Python - octal literals in Python 2.X and Python 3.X


Python 2.X users can code octals with simply a leading zero, the original octal format in Python:


print( 0o1, 0o20, 0o377 )     # New octal format in 2.6+ (same as 3.X) 
#print( 01, 020, 0377 )        # Old octal literals in all 2.X (error in 3.X)


In 3.X, the syntax generates an error.

Python 2.X will treat a string of digits with a leading zero as base 8.

010 is always decimal 8 in 2.X, not decimal 10.

In 3.X you must use 0o010 to present octals.

Related Topic