How to Make Long Numbers Easier to Read in Code

When working with large numbers in code, readability matters — especially when numbers have five or more digits. Just as we often group digits in threes when writing numbers in everyday life (e.g. 1,000,000), it’s helpful to do something similar in code.

In Python, you can improve the readability of numeric literals by using underscores ( _ ) to separate groups of digits. This works with integers, floating-point numbers, and complex numbers. It also applies to binary, octal, and hexadecimal integer formats.

There are no fixed rules about where underscores must be placed—you’re free to insert them wherever it makes the number easier to read.

Here are some examples:

For more information about literals, see the chapter „Everything Is an Object!” in the e-book Python Knowledge Building Step by Step, specifically the subsection „How to Create Objects?”

Interested in the e-book Python Knowledge Building Step by Step: From the Basics to Your First Desktop Application?