Search code examples
pythontype-conversioncomplex-numbers

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)

Solution

  • 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)