# Python – Convert String/Number to Complex Number – complex() Function with Examples

`complex()` function is used to find the a complex number in Python for the real and imaginary parts as the given inputs.
`complex()` function is a part of Python Built-in Functions.

## Syntax of `complex()` Function

The syntax of `complex()` function in Python is:

```complex([real[, imag]])
```

## Parameters of `complex()` Function in Python

`complex()` takes two parameters.
`real` – Where `real` is a real part and its default value is `0` if `real` is omitted.
`imag` – Where `imag` is a imaginary part and its default value is also `0` if `imag `is omitted.

## Compatibility

`complex()` function is available and compatible with both Python `2.x` and `3.x`.

## Return Value of `complex()` Function in Python

`complex()` function returns a complex number with the value in the form of `real + imag*1j` for the given inputs.

## `complex()` Function Exception

`complex()` function throw a `ValueError` exception if the passed string is not a valid complex number.

## Python `complex()` Function Example 1

```x = complex(6, -2)
print(x)

x = complex(1)
print(x)

x = complex()
print(x)

x = complex('8-3j')
print(x)
```

Output:
```(6-2j) (1+0j) 0j (8-3j) ```

## Python `complex()` Function Example 2

```x = 9+2j
print('x =',x)
print('Type of x is',type(x))

y = -6j
print('y =',y)
print('Type of y is',type(y))

z = 0j
print('z =',z)
print('Type of z is',type(z))
```

Output:
```x = (9+2j) Type of x is <class 'complex'> y = (-0-6j) Type of y is <class 'complex'> z = 0j Type of z is <class 'complex'> ```