user10110573
user10110573

Reputation:

How to convert string into complex number in python?

I want to create a special calculator using python but for doing this I should know more about complex numbers. My calculator must get a string and output complex equation. Please help me.

myinput = "5 + 2j"
myoutput = ConvertToComplex(myinput) * (3 + 12j)  #please help me with writing this function
print(myoutput)

Upvotes: 1

Views: 976

Answers (1)

Jannes
Jannes

Reputation: 101

The function you are looking for is already built into python. You just need to get rid of whitespace first:

myinput = "5 + 2j"
mycleaninput = myinput.replace(" ","")
myoutput = complex(mycleaninput) * (3 + 12j)
print(myoutput)

Upvotes: 4

Related Questions