[Numpy-discussion] Printing formatted numerical values

Dave Hirschfeld dave.hirschfeld@gmail....
Mon Nov 15 09:44:11 CST 2010

 <pv+numpy <at> math.duke.edu> writes:

> Hi, what is the best way to print (to a file or to stdout) formatted 
> numerical values? Analogously to C's printf("%d %g",x,y) etc?

For stdout you can simply do:

In [26]: w, x, y, z = np.randint(0,100,4)

In [27]: type(w)
Out[27]: <type 'numpy.int32'>

In [28]: print("%f %g %e %d" % (w,x,y,z,))
68.000000 64 1.300000e+01 57

In [29]: w, x, y, z
Out[29]: (68, 64, 13, 57)

For a file I would use np.savetxt


More information about the NumPy-Discussion mailing list