[Tutor] How to print complex numbers without enclosing parentheses

W W srilyk at gmail.com
Fri Sep 19 12:46:16 CEST 2008


On Fri, Sep 19, 2008 at 4:26 AM, Stephen McInerney
<spmcinerney at hotmail.com>wrote:

>
> Why does the complex.__str__() method on complex numbers add the enclosing
> parentheses?
> It's unwanted, and it also makes them look like a tuple (other than the
> trailing comma).
> How can I get rid of it, other than the clunky:
>
> >>> print d
> (0.80-0.58j)
> >>> print repr(d)[1:-1]
> 0.80-0.58j
>
> How can I change complex.__str__() ?
>

>>> def printComp(comp):
...     comp = str(comp)
...     comp = comp.strip(')')
...     comp = comp.strip('(')
...     print comp
...
>>> printComp(d)
0.8-0.58j


That's the best I could come up with...
HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080919/6fe87130/attachment.htm>


More information about the Tutor mailing list