Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Class

complex

complex() converts a string or number to a complex number, or constructs one from real and imaginary parts. It also supports documented numeric conversion protocols for general objects.

How it works

The built-in complex type converts one string or number into a complex number, or combines real and imaginary numeric arguments. With two arguments, the result represents the supplied components; with one object, conversion can use its numeric conversion protocol.

Syntax

class complex(real=0, imag=0)

Parameters

NameTypeDescription
realobjectA string or number to convert, or the real component when constructing from two values.
imagobjectThe imaginary component; when supplied, it must be numeric and cannot be a string.

Returns

A complex number.

Key points

  • Use complex(value) to convert one input, or complex(real, imag) to construct a value from two components.
  • With two real numeric arguments, the first becomes the real component and the second becomes the imaginary component.
  • When both inputs are complex, construction follows complex arithmetic rather than simply copying their visible components.
  • Custom objects can define __complex__() to control conversion by complex(value).
  • Objects without __complex__() may still convert through their floating-point protocol.
  • The final documented conversion fallback is the integer-index protocol.

Example: Create and parse complex numbers

z = complex(3, 4)
print(z.real, z.imag)
print(complex("-1+2j"))

Creates a complex value from real and imaginary parts, then parses a string representation.

When to use it

Model complex-valued data

Represent values with both real and imaginary components, such as signals or phasors.

Parse complex literals

Turn user or configuration text such as "-1+2j" into a complex value.

Support numeric interoperability

Convert compatible custom numeric objects through Python's numeric protocols.

Interview questions

QWhat forms of input does complex() accept?

complex() accepts either one string or numeric value, or real and imaginary numeric arguments. Two arguments produce a value representing those components.

Follow-up: What happens for a custom object? The conversion delegates to __complex__(), then can fall back to __float__() and __index__().

QHow are the real and imaginary parts constructed?

For real arguments real and imag, complex(real, imag) represents real + imag·1j. A single string is interpreted as a complex-number representation.

Follow-up: Can a string be used as the second argument? No; the second parameter cannot be a string.

QHow does complex() convert a general object?

For a general object, complex(x) first delegates to x.__complex__(). If that is unavailable, conversion falls back to __float__(), then __index__().

Follow-up: What if two arguments are supplied? Each may be any numeric type, including complex, with the documented component formulas applied.

  • float()
  • int()
  • numbers.Complex
  • __complex__()
  • __float__()
  • __index__()

Source: Built-in Functions — Python 3.16.0a0 documentation. Checked 2026-10-16.