October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Write Large Numbers in Python With Underscores

Python lets you group digits in numeric literals with underscores for readability. Learn the placement rules, base-prefix examples, and how output formatting differs.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use a single underscore between digits to make a long Python integer literal easier to read: population = 1_234_567_890. Python ignores the separators when it determines the value, so this has the same value as 1234567890. Underscores in source code and separators added to displayed output are separate features.

How underscores work in Python number literals

Underscores are visual separators in a numeric literal, not arithmetic operators or part of a special number type. For example, 100_000_000_000, 100000000000, and 1_00_00_00_00_000 all represent the same integer. The Python 3.14.8 Language Reference documents the syntax and notes that it has been supported since Python 3.6 (Numeric literals).

Where to put underscores

For decimal integer literals, put one underscore between digits. Python does not require groups of exactly three digits; choose groupings that make the value easiest to interpret in context. Thousands-style grouping is familiar for decimal values:

population = 1_234_567_890
budget_cents = 25_000_000

Other bases support separators too. Use the explicit prefixes 0b for binary, 0o for octal, and 0x for hexadecimal. Group binary digits by meaningful bit patterns, or hexadecimal digits into bytes or words when that suits the value:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
mask = 0b_1111_0000
permissions = 0o_755
address = 0xCAFE_F00D

An underscore may appear immediately after a base prefix, as in 0x_1f. PEP 515, which introduced this readability feature in Python 3.6, describes grouping decimal values by thousands and splitting binary or hexadecimal values into meaningful parts (PEP 515).

Which underscore placements are invalid?

The separator must follow the literal’s grammar; it cannot be placed arbitrarily.

  • _123 is invalid as an integer literal because the underscore comes before the first digit.
  • 321_ is invalid because the underscore trails the digits.
  • 123__321 is invalid because the underscores are doubled.
  • 0_x1f and 0x__1f are invalid; the valid prefixed example is 0x_1f.
  • 0123 is invalid as a nonzero decimal integer literal with a leading zero. Use an explicit base prefix when you mean binary, octal, or hexadecimal.

A sign is not part of the literal itself: in -1, Python applies the unary minus operator to the literal 1.

How to add separators when displaying a number

Underscores in source code make a literal easier to inspect, but they do not automatically change how a value is printed. To format an integer with grouped digits, use the format mini-language’s underscore option:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
large_value = 1_234_567
print(f'{large_value:_}')  # 1_234_567

In the Python 3.14.8 format specification, decimal integer and floating-point presentation types group digits in threes; binary, octal, and hexadecimal integer presentation types group digits in fours. The locale-aware n presentation type is a separate formatting choice. See the Format Specification Mini-Language documentation.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When this feature is useful

Use separators when they make a literal’s magnitude or structure clearer to someone reading the code. For a decimal count, conventional thousands grouping is often easy to scan. For bit masks and hexadecimal constants, group around meaningful bit, byte, or word boundaries. These are readability conventions, not required group sizes; PEP 515 introduced the syntax to support that flexibility.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.