Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk2 min

Python complex() Function: A Practical Guide

Use Python’s complex() to parse a valid complex-number string, convert a number, or construct a value from real and imaginary parts. See syntax, examples, whitespace rules, and version notes.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

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:

  1. x.__complex__(), if available.
  2. x.__float__(), if no complex conversion method is available.
  3. 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What 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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.