Printing complex numbers
tim.one at home.com
Sat Dec 15 19:57:19 CET 2001
> I know that for floats you can do:
> print '.3f' % x
> print '.3e' % x
You need "%" signs in there.
> But how do you do the same for complex numbers?
There isn't a special format code for complex; so print x.real and x.imag
>>> x = 12.5+3.24j
>>> print "%.3f%+.3fj" % (x.real, x.imag)
If you want "the usual" str() or repr() form of a complex number, just use
%s or %r (respectively) in the format.
>>> print "%s" % x
More information about the Python-list