The function print()
internally calls the representation str()
(or method __str__()
) of the object, while the interactive mode is using the function repr()
(or the __repr__()
method). The first one offers a "human-friendly" representation of the object, while the second one is more destined to the computer and in principle it should be evaluable by the interpreter to recover the original object.
If we run everything in interactive mode:
In [1]: import numpy as np
In [2]: a = np.array([10, 20, 30, 40])
In [3]: a
Out[3]: array([10, 20, 30, 40])
In [4]: print(a)
[10 20 30 40]
In [5]: str(a)
Out[5]: '[10 20 30 40]'
In [6]: a.__str__()
Out[6]: '[10 20 30 40]'
In [7]: repr(a)
Out[7]: 'array([10, 20, 30, 40])'
In [8]: a.__repr__()
Out[8]: 'array([10, 20, 30, 40])'
Here more information in English about the differences between str()
and repr()
.