Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Repair Windows errors before they cause bigger problemsFix Now →Python’s built-in complex() converts a supported string or number into a complex value, or constructs one from real and imaginary parts. Use complex(3, 4) for (3+4j), or complex('3+4j') to parse a string. This guide follows the Python 3.14 built-in-functions reference.
What does Python complex() do?
complex() is both a conversion function and a constructor. A complex value has a real part and an imaginary part; Python writes the imaginary unit with a j or J suffix. The components are available through the .real and .imag attributes.
z = complex(3, 4)
print(z) # (3+4j)
print(z.real) # 3.0
print(z.imag) # 4.0
Python documents complex numbers as having real and imaginary parts that are each floating-point numbers. See the Python 3.14.8 numeric types reference.
Choose the right call form
| Call form | Use it for | Example | What can go wrong |
|---|---|---|---|
| One string argument | Parsing a complex number written as text | complex('3+4j') gives (3+4j) |
Malformed syntax, including disallowed internal spaces, raises ValueError. |
| One numeric argument | Converting a number to a complex value with zero imaginary part | complex(1.23) gives (1.23+0j) |
The object must support the documented conversion protocol. |
| Two numeric arguments | Supplying the real part first and imaginary part second | complex(-1.23, 4.5) gives (-1.23+4.5j) |
Use numeric arguments; the two-argument form is not the string-parsing form. |
| No arguments | Creating the zero complex value | complex() gives 0j |
None for this form. |
Parse a complex number from a string
A single string may represent a real value, an imaginary value, or both. For a combined value, write an explicit sign before the imaginary component. The suffix for the imaginary component is j or J.
#1 Best Overall
complex('3') # (3+0j)
complex('4j') # 4j
complex('3+4j') # (3+4j)
complex('3-4J') # (3-4j)
complex(' (3+4j) ') # (3+4j)
Whitespace around the entire expression and parentheses around it are allowed. Arbitrary whitespace inside the expression is not: complex('1 + 2j') raises ValueError. Keep the numeric expression together, as in '1+2j'.
Why does complex(‘1 + 2j’) raise ValueError?
The string parser does not accept spaces inserted between the real component, operator, and imaginary component. Removing those internal spaces fixes this case: complex('1+2j'). Outer whitespace, such as in ' 1+2j ', is a different matter and is permitted.
Rank #2
Convert numbers and other objects
With one numeric argument, complex(x) converts the value and supplies a zero imaginary part when appropriate. For a general object, Python tries conversion methods in this order:
x.__complex__(), if available.x.__float__(), if no complex conversion method is available.x.__index__(), as the final fallback.
The __index__() fallback is documented as a change introduced in Python 3.8. For custom classes, implementing __complex__() is the direct way to define complex conversion; the fallback methods let suitable objects provide a real-valued conversion instead.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Fix the driver behind crashes, sound loss and screen glitches3Clear out junk files and repair common Windows errorsWhat can you do with the result?
Complex values support ordinary arithmetic, including addition, subtraction, multiplication, division, and exponentiation. They do not support ordering comparisons such as < or >; operations such as floor division are also unavailable for complex values. If a calculation needs a magnitude or a comparison, work with an explicitly chosen property or derived quantity rather than trying to order the complex values themselves.
Python 3.14 note: complex.from_number()
The Python 3.14 built-in-functions reference also documents complex.from_number(x), added in Python 3.14. It is a separate class method, not another spelling of the built-in call forms described above. Consult the versioned Python 3.14 reference when using it, and check the Python version in an environment that may run an earlier release.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




