Skip to main content

How to Write Large Numbers with Underscores in Python

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Is 1000000 a million or a hundred thousand? You have to count the zeros. Python lets you write it as 1_000_000 instead, and the underscores change nothing about the number. This guide shows it on Python 3.15.2.

Syntax​

Syntax
<digits>_<digits>
PartWhat it means
<digits>One or more digits of the number
_A single underscore between two digits. Python ignores it when it works out the value

The Python language reference says: "Underscores can be used to group digits for enhanced readability, and are ignored for determining the numeric value of the literal." It adds one rule: "Underscores can only occur between digits."

The Example​

A variable holds one million, written as 1_000_000. The script prints it, compares it with 1000000, and adds 1 to it.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

big_numbers.py
tokens = 1_000_000

print(tokens)
print(tokens == 1000000)
print(tokens + 1)

The big_numbers.py script in a macOS-style code window: tokens is set to 1_000_000, then the script prints tokens, whether it equals 1000000, and tokens + 1.

Step 2: Run It​

Terminal
uv run python big_numbers.py
Output
1000000
True
1000001

The terminal after uv run python big_numbers.py: 1000000, then True, then 1000001.

Python printed 1000000, without underscores: they exist only in your code, not in the number. The comparison is True, because 1_000_000 and 1000000 are the same number, and adding 1 works as it does for any number. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Two underscores in a row: 1__000SyntaxError: invalid decimal literalUse a single underscore between digits
An underscore at the end: 1000_SyntaxError: invalid decimal literalPut underscores only between digits
Expecting print() to show the underscoresprint(tokens) shows 1000000Format the number: f"{tokens:_}" gave 1_000_000, and f"{tokens:,}" gave 1,000,000

FAQ​

Does it work for numbers with a decimal point?​

Yes. 2_500.75 gave 2500.75. The reference says: "As in integer literals, single underscores may occur between digits to help readability".

Can int() read text with underscores?​

Yes. int("1_000_000") gave 1000000.

Conclusion​

tokens = 1_000_000 stored one million. Python printed it as 1000000, it compared equal to 1000000, and adding 1 gave 1000001.

Additional Resources​