You format scientific notation in Python by choosing the right layer first: f-strings and format() for everyday display, Decimal when trailing zeros and exact arithmetic matter, and NumPy or pandas formatters when you're shaping arrays and tables. The key condition is that each layer treats the exponent, the mantissa digits, and the sign differently, so pick based on how the number will be read — a fixed-width column, a report caption, a data interchange file — rather than by habit.
Nothing here requires extra libraries beyond the standard library unless you're formatting arrays (numpy) or tables (pandas). All examples assume Python 3.6 or later, since f-strings are the cleanest entry point and the reference date for this article is November 2026.
The format-spec skeleton that drives everything
Every scientific-notation string in Python's format mini-language follows the same pattern:
{value:[fill][align][sign][width][,][.precision][type]}
For scientific notation, the type field is e (lowercase) or E (uppercase), and .precision means digits after the decimal point, not total significant figures. That distinction is the single most common source of confusion, so lock it in early: .2e gives two digits after the decimal, producing three significant figures total in the mantissa.
Two behaviors worth noticing. First, the default precision for e and E is 6, which is more than most people expect. Second, the exponent always carries a sign and at least two digits: e-04, not e-4. That padding is deliberate — it keeps columns aligned — and it's not configurable through the format spec. If you need 1.23e-4 you have to post-process the string.
Rounding uses round-half-to-even on the underlying binary value, so 2.5e0 with .0e becomes 2e+00, not 3e+00. For money or anything where tie-breaking matters legally or by convention, that's a reason to work with Decimal instead of float.
Formatting floats for display: alignment, signs, and cleanup
The practical problems with scientific output are rarely about producing an exponent. They're about making a column of numbers readable. Here's where the other spec fields earn their keep.
Output:
The sign field controls whether positive numbers show a +. f"{v:+.2e}" gives +1.20e-09, which is useful when you're comparing deltas and the reader needs symmetry. f"{v: .2e}" reserves a space for the sign so positive and negative values occupy the same width without a visible plus.
A subtle trap: width counts the whole string including exponent and sign. If your width is too small, Python doesn't truncate — it silently overflows the column and breaks alignment. Check your widest actual value before choosing a width, or compute it:
When you want the exponent to read naturally without a leading zero — 1.23e-4 rather than 1.23e-04 — a small regex is the standard approach:
Note that this changes your output from a fixed width to a variable one, which defeats column alignment. Use it for inline prose, not tables.
Choosing the type letter: when e, E, g, and G differ
e isn't the only route to scientific notation. The g and G types use e notation when the exponent is small or large enough and fixed-point otherwise, based on the precision you supply. The rule in CPython is: g switches to scientific form when the decimal exponent is less than -4 or greater than or equal to the precision value.
That makes g attractive when you want "scientific only when needed," which is common in tables where most values are moderate and a few are extreme. The cost: a g-formatted column mixes fixed and exponential notation, which can look inconsistent. If consistency matters more than compactness, use e everywhere; if compactness matters more, use g and accept the mix.
Also relevant: g strips trailing zeros from the mantissa by default, while e pads to your specified precision. f"{1.0:.4g}" gives '1', but f"{1.0:.4e}" gives '1.0000e+00'. Neither is wrong — they serve different readers.
Integers, Decimal, and exactness
Passing an int to e-formatting works, but it converts internally to float, which means very large integers lose precision. Python's int has arbitrary precision; float does not. For a number like 10400, f"{10400:e}" raises OverflowError.
When exactness or trailing-zero control matters, use Decimal:
Notice the exponent format: Decimal's e type produces e+0, e+3, and so on — no minimum two-digit padding, unlike float. That's a real inconsistency between the two types and something to normalize if you're mixing them in one report. The rule to remember: float exponents are always at least two digits; Decimal exponents are not padded.
You can also control rounding directly on Decimal using the rounding parameter of quantize, or by passing Decimal context settings. If a regulator or a style guide specifies round-half-up, that capability is the reason to reach for Decimal rather than fight float's half-to-even behavior.
Formatting numbers inside strings and f-strings with precision from variables
Hardcoded precision is fine for a single print, but reports usually need the precision to come from somewhere — a settings file, a CLI argument, a column definition. Nest the precision in the spec:
This works because the spec itself is a format string, so {} inside it is evaluated first. The same trick applies to width and to the fill/align characters.
When you need to assemble the whole spec dynamically, format(value, spec) and str.format both accept it identically. Prefer f-strings when the spec is static or lightly parameterized; build the spec string explicitly when it comes from configuration, because that makes the configuration visible and testable.
One more everyday pattern: mixed units. If your values are in volts and you want millivolts in scientific notation, scale first, then format — don't try to encode the unit in the format spec.
NumPy, pandas, and arrays
For arrays, Python's elementwise formatting is slow and verbose; NumPy has its own mechanism through numpy.format_float_scientific:
This accepts unique, trim, sign, pad_left, exp_digits, and precision — which means you can finally control exponent padding here, unlike the plain format spec. trim='k' removes trailing zeros; unique=True drops digits that are identical across the array for a cleaner display. If you're printing a NumPy array with np.set_printoptions, formatter={'float_kind': lambda x: np.format_float_scientific(x, precision=2)} gives you array-wide control.
Pandas has two separate tools: DataFrame.to_string(float_format=...) for text output and Styler.format for notebook display. Both take a callable or a format string. A format string is faster and less flexible; a callable lets you branch on magnitude, which is where you'd implement a "scientific above 1e6, fixed below" rule.
Be aware that float_format in to_string applies to all float columns uniformly, and Excel export via to_excel ignores float_format entirely — a documented limitation, not a bug. If a spreadsheet consumer needs specific formatting, that's a formatting job inside Excel, not something pandas can enforce for you.
Troubleshooting: matching symptoms to causes
When output isn't what you expected, the cause is usually one of a handful of things, and they're distinguishable by symptom.
If the output looks like nan or inf, the underlying value is float NaN or infinity; e-formatting handles these as literal strings and they ignore precision and width in ways that can break alignment. Guard before formatting.
If the exponent appears as e+04 when you wanted e4, that's the two-digit padding rule and requires regex cleanup or NumPy's exp_digits. Reversible check: format a single value in isolation and inspect it.
If numbers round "wrong" at the midpoint, you've hit banker's rounding. Confirm by testing f"{2.5:.0f}" and f"{3.5:.0f}" — both round to even. If you need different behavior, switch to Decimal.
If a width spec causes your column to overflow, the value is wider than the width — Python never truncates silently on numeric formatting, it just overflows. Compute the maximum width from your data, as shown earlier.
If you get a ValueError for the format spec, you've likely mixed a type with an incompatible option — , grouping with e, for instance, is invalid. Stop and remove the grouping flag rather than trying variations; the grammar is fixed and error messages in modern Python quote the offending spec.
Where further work genuinely needs a professional: if you're producing registered financial statements, regulatory filings, or safety-critical instrumentation readouts, have a domain specialist confirm the rounding convention, significant-figure rules, and unit scaling before you deploy. Engineering a formatter that looks right is a coding problem; certifying that it is right for a governed output is a compliance problem, and no amount of testing substitutes for the applicable standard.
A worked end-to-end example
Bring the pieces together for a plausible task: a CSV report of measurements where the third column must always be scientific with four significant figures, right-aligned in 14 characters, and signed.
Success check: read the file back with csv.reader and assert each value matches a regex like [+-]\d.\d{3}e[+-]\d{2,}. That assertion is your stop condition for formatting correctness in the report; if it fails, fix the spec before publishing, and if the report feeds a governed process, route the fix through whoever owns the reporting standard.
The Decimal step is doing real work here. Going straight from float to .3e risks a tie-breaking surprise on a value like x.x5; converting to a Decimal at ample precision and then applying the display precision makes the behavior predictable and reproducible from a written spec. It also means you can hand the Decimal value directly to an export pipeline that requires exactness, rather than losing precision at the display boundary.
Practical defaults worth adopting
For a quick print, f"{x:.3e}" is a reasonable default: three decimal places is enough to distinguish most measurements and short enough to read. For a column, pick width from your data and add >. For shared reports, drive precision from a variable so one change propagates. For exact or regulated output, switch to Decimal and specify rounding. For arrays, use np.format_float_scientific and let it handle exponent padding. And whenever you notice a formatting decision that a reader might question, write the spec down explicitly next to the output — the format string is documentation that happens to execute.
The most reliable habit is the smallest one: print a single representative value before formatting the batch, look at whether the exponent padding, trailing zeros, and sign match what the reader expects, and only then scale up. Every error in this article surfaces at that one-value check, before it can reach a report.