Python complex() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Built-in type

Definition

In Python, complex(real, imag) creates a complex number with a real part and an imaginary part: complex(3, 4) is (3+4j). You can also write the literal 3 + 4j, where j marks the imaginary unit.

complex("1+2j") parses a string, which must not contain spaces around the + or -. Both parts are stored as floats and are available as .real and .imag; abs() gives the magnitude.

complex is a built-in type (a class) that you call like a function: each call creates a new complex object. Like every built-in, it lives in the builtins module and is always available without an import.

Syntax

Give the two parts as numbers, or one string.

python
z = complex(real, imag)
z = complex("1+2j")
CallWhat it does
complex(3, 4)Returns (3+4j).
complex(2)Returns (2+0j).
complex("1-2j")Returns (1-2j).
complex()Returns 0j.

Use the cmath module for square roots and other math on complex numbers; math.sqrt(-1) raises ValueError.

Library declarations
python
complex(real=0, imag=0)
complex(string, /)
# class complex: complex is a built-in type

Parameters

ParameterDescription
realOptional. The real part (a number), or a string to parse. Default 0.
imagOptional. The imaginary part. Not allowed when the first argument is a string. Default 0.

Return Value

Return ValueDescription
complexA complex number whose .real and .imag are floats.

Python Examples

Example 1 — Create a Complex Number and Read Its Parts

Build 3+4j, read both parts, and get its magnitude and conjugate.

python
z = complex(3, 4)

print(z)
print(z.real, z.imag)
print(abs(z))
print(z.conjugate())
print(complex(2), complex())

How It Works

  1. Printed form. A complex number prints in parentheses with j for the imaginary part.
  2. Float parts. .real and .imag are floats, so they print as 3.0 and 4.0.
  3. Magnitude and conjugate. abs(z) is 5.0 (a 3-4-5 triangle) and conjugate() flips the sign of the imaginary part.
  4. Defaults. A missing imaginary part is 0, and complex() with no arguments is 0j.

Example 2 — Add and Multiply Complex Numbers

Parse one number from a string, then add, multiply, and square the imaginary unit.

python
a = 1 + 2j
b = complex("3-1j")

print(a + b)
print(a * b)
print(1j * 1j)
print(a == complex(1, 2))

How It Works

  1. String input. complex("3-1j") parses a number written without spaces.
  2. Addition. Real parts and imaginary parts add separately: (1 + 3) and (2 - 1) give (4+1j).
  3. Multiplication. (1 + 2j)(3 - 1j) = 3 - 1j + 6j - 2j², and since j² is -1 the result is (5+5j).
  4. j squared. 1j * 1j prints (-1+0j), and a literal equals the same value built with complex().

Example 3 — Solve a Quadratic with Complex Roots

Use cmath.sqrt() on a negative discriminant, then see the string format rule.

python
import cmath

a, b, c = 1, 2, 5
disc = b * b - 4 * a * c
root1 = (-b + cmath.sqrt(disc)) / (2 * a)
root2 = (-b - cmath.sqrt(disc)) / (2 * a)
print(disc)
print(root1, root2)

try:
    complex("1 + 2j")
except ValueError:
    print("no spaces allowed inside the string")

How It Works

  1. Negative discriminant. For x² + 2x + 5, b² - 4ac is 4 - 20 = -16, so there are no real roots.
  2. cmath.sqrt. Unlike math.sqrt, it accepts -16 and returns 4j.
  3. Conjugate pair. (-2 ± 4j) / 2 gives the roots (-1+2j) and (-1-2j).
  4. String format. "1 + 2j" has spaces, so complex() raises ValueError.

Frequently Asked Questions

Write a literal like 3 + 4j, or call complex(3, 4). Both create the same value.
Python follows the electrical engineering convention, where i usually means current. The imaginary unit is written 1j.
Use z.real and z.imag. Both are floats.
The string form must not contain spaces around + or -. Use complex("1+2j").
Use cmath.sqrt(-16), which returns 4j. math.sqrt() only works with non-negative real numbers.

Next: delattr()

Delete an attribute by name.

delattr() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful