Leading Zeros in Python: A Hidden Mystery Revealed

Interesting Python Tips & Tricks

Estefania Cassingena Navone
Techmacademy
4 min readFeb 5, 2019

--

👋 Welcome!

✔️ Did you know that Python 3 doesn’t accept leading zeros for integers?
✔️ Did you know that Python 2 did accept leading zeros?
✔️ Do you want to know why? 👍

In this article, you will learn exactly why and what changed. You’ll be surprised to know that it has to do why the octal numeral system.

Amazing, right? 😄 Let’s dive into the details!

✅ Python 2 — Leading Zeros… Here We Go!

In this version of Python, you were able to add leading zeros to your liking without causing any errors. Expressions like these were allowed:

print(000000000000000015)
print(000000000000000025.6)
print(053)
print(-00004)
var1 = 0075

I’m sure that you must be thinking: why would they allow this if leading zeros don’t add anything to the result?

0000015 should be equivalent to 15, right? 😟

⚠️ Not in Python 2! 😱

The first zero in 0000015 acts like a “flag” 🚩 that tells the Python interpreter that the number is expressed in the octal numeral system (base 8) instead of the decimal system that we usually work with (base 10).

# In Python 2
>>> 0000015
13

This is why the interpreter returns the number 13 when we type 0000015. The number is converted from the octal numeral system to the decimal numeral system. (I included a brief explanation of this process below 👍).

But how can we tell Python that this number is already in the decimal numeral system, and that the leading zeros shouldn’t change the result?

This is where Python 3 comes to the rescue! 🎊

✅ Python 3— Bye, Bye, Leading Zeros!

In Python 3, this is not a problem anymore because the “flag” 🚩 is much clearer now to differentiate numbers in the octal numeral system. The number must start with 0o or 0O (zero followed by an “o” letter in lowercase or uppercase).

>>> 0o0000015  # Notice the "flag" 0o
13
>>> 0O0000015
# Notice the "flag" 0O
13
>>> 0000015
# We didn't add the "flag", so an error is thrown
SyntaxError: invalid token

In the last example, you can see that in Python 3, if we try to write an integer with leading zeros, an error is thrown because this version already implemented a way to differentiate both numeral systems.

💡 Tips: In Python 3, decimal numbers and zeros do accept leading zeros.

>>> 000000000000000  # A sequence of zeros is transformed into 0
0
>>> 000005.6
# A decimal number
5.6

✅ Quick Tips: Octal Numeral System

I’m sure that you must be curious about this numeral system, so let’s analyze how 0000015 becomes 13 in Python 2. 😄

1️⃣ Decimal System

This is the numeral system that we usually use in practice. It has base 10, which means that each digit is multiplied by 10 raised to a power to obtain the final result. The digits allowed in the decimal numeral system are 0–9.

For example:
1365 in the decimal numeral system is 1*(10³)+3*(10²)+6*(10¹)+5*(10⁰)
This is equivalent to 1000 + 300 + 60 + 5 = 1365.

See how we multiply each digit by 10 raised to a power starting from 0 and increasing by one for each place to the left?

2️⃣ Octal System

In contrast, numbers in the octal numeral system have base 8. This means that instead of multiplying by 10 raised to a power, we multiply by 8 raised to a power to transform the number to its equivalent number in the decimal system. The digits allowed in the octal numeral system are 0–7.

For example:
2536 in the octal numeral system is 2*(8³) +5*(8²)+3*(8¹)+6*(8⁰).
This is equivalent to 1024 + 320 + 24 + 6 = 1374 in the decimal system.

>>> 02536 # In Python 2, the first 0 is the flag.
1374

💡 Note: Using a digit greater than 7 would cause an error.

# Example in Python 3
>>> 0o0008
SyntaxError: invalid syntax

👋 Thank you!

I really hope that you liked my article. ❤️
I sincerely appreciate your claps and comments.👏
Follow me on Medium | Twitter to find more articles like this. 😃

--

--

Estefania Cassingena Navone
Techmacademy

Udemy Instructor | Developer | MITx 6.00.1x Community TA | Writer @Medium | CS & Mathematics Student | WTM Udacity Scholar