Printing Arrays

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

This page was checked in October 2026 against NumPy 2.5 (2.5.4 is the current patch release). Examples target Python 3.13 or 3.14 and were run on Python 3.13. Every example was run on NumPy 2.5.4.

NumPy decides how an array looks when it is printed with a handful of global-looking print options, which you can change temporarily with a context manager. Printing only changes the text; it never changes the stored values. This page lists every option, the two string forms (str and repr), the NumPy 2 change to scalar representations, and the functions that give you full control.

str and repr

print(arr) uses str(arr), which is meant for people, and a notebook cell or a REPL uses repr(arr), which includes array(...) and a dtype= when the dtype is not the default for the values. For a scalar, str is the plain number but, since NumPy 2.0 (NEP 51), repr shows the type:

import numpy as np

print(np.float64(3.0), repr(np.float64(3.0)))
print(repr(np.array([1.5, 2])), repr(np.array([1, 2], dtype="i1")))
with np.printoptions(legacy="1.25"):
    print(repr(np.float64(3.0)))
3.0 np.float64(3.0)
array([1.5, 2. ]) array([1, 2], dtype=int8)
3.0

Code and documentation written for NumPy 1.x that show 3.0 as the repr of a scalar, for example in doctests, fail on NumPy 2. Either update the expected output or select the old form with legacy="1.25". Always use float(x) or x.item() when you need a plain Python value, and an f-string format (f"{x:.3f}") when you need a specific appearance.

numpy.array_str(a) and numpy.array_repr(a) give the same strings as str and repr for an array, with arguments for max_line_width, precision and suppress_small. numpy.array2string is the general function (see below).

Summarisation of large arrays

When an array has more than threshold elements (default 1000), NumPy prints only edgeitems items (default 3) at the start and end of each axis, with ... between them:

a = np.arange(2000).reshape(40, 50)
print(a)
[[   0    1    2 ...   47   48   49]
 [  50   51   52 ...   97   98   99]
 [ 100  101  102 ...  147  148  149]
 ...
 [1850 1851 1852 ... 1897 1898 1899]
 [1900 1901 1902 ... 1947 1948 1949]
 [1950 1951 1952 ... 1997 1998 1999]]

Change this temporarily with np.printoptions, which is a context manager that restores the previous settings, even if the block raises:

import sys

with np.printoptions(threshold=10, edgeitems=2):
    print(np.arange(100))              # [ 0  1 ... 98 99]

with np.printoptions(threshold=sys.maxsize):
    print(np.arange(12).reshape(3, 4)) # never summarise

with np.printoptions(linewidth=30):
    print(np.arange(20))
[ 0  1 ... 98 99]
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
[ 0  1  2  3  4  5  6  7  8
  9 10 11 12 13 14 15 16 17
 18 19]

threshold=sys.maxsize disables summarisation. In the repr of a summarised array NumPy 2.2 and later adds the shape (array([0, 1, 2, ..., 7, 8, 9], shape=(10,))). Printing a very large array in full is slow and floods a terminal or notebook; for inspection, print a slice (a[:5]) or a.shape and a.dtype, and write the full array to a file with np.savetxt or np.save.

Print options

np.get_printoptions() returns the current options as a dictionary; these are the defaults in NumPy 2.5:

print(np.get_printoptions())
{'edgeitems': 3, 'threshold': 1000, 'floatmode': 'maxprec', 'precision': 8, 'suppress': False, 'linewidth': 75, 'nanstr': 'nan', 'infstr': 'inf', 'sign': '-', 'formatter': None, 'legacy': False, 'override_repr': None}
Option Meaning Default

precision

Digits after the decimal point for floats.

8

threshold

Total number of elements above which the array is summarised.

1000

edgeitems

Items shown at each end of an axis when summarising.

3

linewidth

Characters per line before wrapping.

75

suppress

Use fixed notation instead of scientific for small values (see below).

False

floatmode

How many digits to print: fixed, unique, maxprec, maxprec_equal.

maxprec

sign

"-" only negative sign, "+" always a sign, " " a space for positive numbers.

"-"

nanstr, infstr

Text for NaN and infinity.

nan, inf

formatter

Dictionary of per-type formatting functions.

None

legacy

Reproduce the printing of an older NumPy version (False, "1.13", "1.21", "1.25", "2.1", "2.2").

False

override_repr

A function that replaces repr of arrays.

None

set_printoptions(...) changes the options for the rest of the session (or until the next call), which is convenient in a notebook but surprising in a library; a library should never call it at import time. In code that must not leak settings, use the np.printoptions(...) context manager.

precision, floatmode and suppress

precision sets the digits after the decimal point, and floatmode decides what to do with them:

  • "fixed": always exactly precision digits, with trailing zeros.

  • "unique": print the shortest digits that identify the value uniquely, ignoring precision.

  • "maxprec" (default): at most precision digits, fewer if the value is exact with fewer digits.

  • "maxprec_equal": at most precision digits, but the same number for every element.

x = np.array([1e-5, 1.23456789, 1000.5, 1e8])
print(x)
with np.printoptions(precision=3):
    print(x)
with np.printoptions(floatmode="fixed", precision=2):
    print(x)
with np.printoptions(floatmode="unique"):
    print(np.array([0.1, 1/3]))
with np.printoptions(floatmode="maxprec_equal", precision=3):
    print(np.array([0.1, 1/3]))
[1.00000000e-05 1.23456789e+00 1.00050000e+03 1.00000000e+08]
[1.000e-05 1.235e+00 1.000e+03 1.000e+08]
[1.00e-05 1.23e+00 1.00e+03 1.00e+08]
[0.1                0.3333333333333333]
[0.100 0.333]

An array switches to scientific notation when the smallest non-zero absolute value is below 0.0001 or the ratio between the largest and smallest exceeds 1000; in that case a single wide column is shown for all elements. suppress=True turns the smallest-value and ratio rule off so small values print in fixed notation, but it does not suppress scientific notation when the largest value is at least 1e8:

y = np.array([1e-5, 1.5, 200.0])
print(y)
with np.printoptions(suppress=True):
    print(y)
with np.printoptions(suppress=True, precision=3):
    print(y)           # 1e-5 is displayed as 0.
with np.printoptions(suppress=True):
    print(np.array([1e8, 1.5]))
[1.0e-05 1.5e+00 2.0e+02]
[  0.00001   1.5     200.     ]
[  0.    1.5 200. ]
[1.0e+08 1.5e+00]

Printing a small value as 0. does not mean the value is zero; compare with y == 0 or look at y.tolist().

sign, nanstr and infstr

print(np.array([np.nan, np.inf, -np.inf, 1.0]))
with np.printoptions(nanstr="NA", infstr="INF"):
    print(np.array([np.nan, np.inf, 1.0]))
with np.printoptions(sign="+"):
    print(np.array([1.0, -2.0]))
with np.printoptions(sign=" "):
    print(np.array([1.0, -2.0]))
[ nan  inf -inf   1.]
[ NA INF  1.]
[+1. -2.]
[ 1. -2.]

formatter

formatter maps a type key to a function that receives one element and returns a string. The individual keys are "bool", "int", "timedelta", "datetime", "float", "longfloat", "complexfloat", "longcomplexfloat", "numpystr" and "object"; the group keys "all", "int_kind", "float_kind", "complex_kind" and "str_kind" set several at once. Types without an entry use the default formatter. Each call to set_printoptions resets formatter. It affects printing only and is the most flexible option:

with np.printoptions(formatter={"float": "{:8.3f}".format}):
    print(x)
with np.printoptions(formatter={"int": lambda v: f"#{v:03d}"}):
    print(np.arange(4))
[   0.000    1.235 1000.500 100000000.000]
[#000 #001 #002 #003]

legacy and override_repr

legacy makes NumPy print as an older release did. "1.13" approximates NumPy 1.13 output (a space in the sign position of floats, different 0-d array behaviour) and also enables "1.21", which removes the spaces after commas and colons that separate fields of complex structured dtypes. "1.25" prints numeric scalars without type information (3.0 instead of np.float64(3.0)). "2.1" omits the shape information in summarised arrays. "2.2" lets the switch to scientific notation for float16 and float32 happen later or never for large values. Unrecognised strings are ignored with a warning. It is a migration aid for doctests and notebooks, not a long-term setting, and False means current behaviour. override_repr takes a function that replaces repr(array) completely and makes the other options irrelevant for it, for example to make a short, deterministic repr in logs:

with np.printoptions(override_repr=lambda a: f"<array shape={a.shape}>"):
    print(repr(np.zeros((2, 3))))
<array shape=(2, 3)>

Full control with array2string and format helpers

numpy.array2string returns the string and takes the same options as arguments (separator, prefix, suffix, precision, max_line_width, threshold, formatter, floatmode and more). prefix makes continuation lines line up under text you place before the array:

print(np.array2string(np.arange(4.), separator=", ", precision=1))
print("arr = " + np.array2string(np.array([[1, 2], [3, 4]]), prefix="arr = "))
print(np.format_float_positional(1/3, precision=4),
      np.format_float_scientific(12345.678, precision=2))
print(np.base_repr(10, base=2), np.binary_repr(5, width=8))
[0., 1., 2., 3.]
arr = [[1 2]
       [3 4]]
0.3333 1.23e+04
1010 00000101

format_float_positional and format_float_scientific format one scalar with the same algorithm NumPy uses for arrays (unique, trim, pad_left); base_repr and binary_repr write integers in another base, with two’s complement for negative numbers in binary_repr(width=...).

Other types and special cases

print(np.array([1+2j, 3.5-1j]))
print(np.array([True, False]), np.array(["a", "bcd"]))
print(np.zeros((0, 3)), repr(np.zeros((0, 3))))
print(np.array(5), repr(np.array(5)))
print(np.array([[1, 2], [3, 4]]).tolist())
import numpy.ma as ma
print(ma.masked_array([1, 2, 3], mask=[0, 1, 0]))
print(f"{np.float64(1/3):.3f}", f"{np.arange(3)}")
[1. +2.j 3.5-1.j]
[ True False] ['a' 'bcd']
[] array([], shape=(0, 3), dtype=float64)
5 array(5)
[[1, 2], [3, 4]]
[1 -- 3]
0.333 [0 1 2]

An f-string format spec such as :.3f works on a NumPy scalar but not on a whole array: format each element, or use formatter. ndarray.tolist() returns nested Python lists whose elements are plain Python numbers, which is useful for JSON or for printing with the pprint module. See Masked Arrays for -- in masked output, and Strings, Bytes & StringDType for string arrays.

For files and tables, np.savetxt is the usual way to write an array with a controlled format (see Reading & Writing Files); in pandas, DataFrame.to_string and pandas.set_option("display.precision", n) play the role of print options.

Pitfalls

  • repr of scalars changed in NumPy 2 (np.float64(3.0)); old doctests break. Use legacy="1.25" temporarily, or format values explicitly.

  • set_printoptions is process-wide in practice: prefer with np.printoptions(...) in libraries and tests, so one call does not change another module’s output.

  • suppress=True can display a small non-zero value as 0.; the value itself is unchanged.

  • precision affects only display. np.round changes values; neither changes the dtype.

  • The summarised ... form hides elements: do not copy numbers out of a printed summarised array.

  • Output depends on linewidth, so snapshot tests that compare printed arrays should fix linewidth and precision with np.printoptions or compare values with np.testing.assert_allclose instead.