How to print a Numpy array without brackets?

You can use the join method from string:

>>> a = [1,2,3,4,5]
>>> ' '.join(map(str, a))
"1 2 3 4 5"

Maybe a bit hacky, but I just slice them off after using np.array2string so:

import numpy as np

a = np.arange(0,10)
a_str = np.array2string(a, precision=2, separator=', ')
print(a_str[1:-1])

Result:

    0, 1, 2, 3, 4, 5, 6, 7, 8, 9

np.array2string has lots of options also, so you can set your column width which can be very helpful with lots of data:

a = np.arange(0,15)
a_str = np.array2string(a, precision=2, separator=', ', max_line_width=15)
print(' ' + a_str[1:-1])

Gives:

       0,  1,  2,
       3,  4,  5,
       6,  7,  8,
       9, 10, 11,
      12, 13, 14

And it will smartly split at the array elements. Note the space appended to the beginning of the string to account for aligning the first row after removing the initial bracket.


np.savetxt

Python 3 (see also):

import numpy as np
import sys

a = np.array([0.0, 1.0, 2.0, 3.0])
np.savetxt(sys.stdout.buffer, a)

Python 2:

import numpy as np
import sys

a = np.array([0.0, 1.0, 2.0, 3.0])
np.savetxt(sys.stdout, a)

Output:

0.000000000000000000e+00
1.000000000000000000e+00
2.000000000000000000e+00
3.000000000000000000e+00

Control the precision

Use fmt:

np.savetxt(sys.stdout, a, fmt="%.3f")

output:

0.000
1.000
2.000
3.000 

or:

np.savetxt(sys.stdout, a, fmt="%i")

output:

0
1
2
3

Get a string instead of printing

Python 3:

import io
bio = io.BytesIO()
np.savetxt(bio, a)
mystr = bio.getvalue().decode('latin1')
print(mystr, end='')

We use latin1 because the docs tell us that it is the default encoding used.

Python 2:

import StringIO
sio = StringIO.StringIO()
np.savetxt(sio, a)
mystr = sio.getvalue()
print mystr

All in one line

Or if you really want all in one line:

a = np.array([0.0, 1.0, 2.0, 3.0])
np.savetxt(sys.stdout, a, newline=' ')
print()

Output:

0.000000000000000000e+00 1.000000000000000000e+00 2.000000000000000000e+00 3.000000000000000000e+00 

TODO: there is a trailing space. The only solution I see is to save to a string and strip.

Tested on Python 2.7.15rc1 and Python 3.6.6, numpy 1.13.3


If you have a numpy array to begin with rather than a list (since you mention a "real numpy array" in your post) you could use re.sub on the string representation of the array:

print(re.sub('[\[\]]', '', np.array_str(a)))

Again, this is assuming your array a was a numpy array at some point. This has the advantage of working on matrices as well.