# Python complex()

Python `complex()` function takes real, imaginary numbers and strings as input and converts them into a complex number. This method takes two optional parameters (real and imaginary) and returns a complex number as output.

## complex() Syntax

The syntax of `complex()` is:

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

## complex() Parameters

The `complex()` method can take two parameters, and both are optional.

• real (optional) – A number representing the real part of the complex number. The real number can be of a string like ‘`3+5j`‘. If not set anything, it defaults to 0.
• imaginary (optional) – A number representing the imaginary part of the complex number. If not set anything, it defaults to 0.

If the first parameter is passed as a string, then it will be interpreted as a complex number. In this case, the second parameter should be omitted.

## complex() Return Value

It returns the complex number as output.

Note: In string conversion, it must not contain whitespace around the operator (+ or -). Example – `complex('1+2j')` is fine, but `complex('1 + 2j') `raises ValueError.

## Example 1: How to create a complex number in Python?

In this example, we will take a look at creating the complex number using real numbers with integer, float and string.

``````# Code to illustrate complex number

# covert real number to complex
num = complex(5,3)
print(num)

# covert real number to complex
num = complex(5,-4)
print(num)

# Default if no parameter is passed to complex method
num = complex()
print(num)

# In case if you pass only real number, defaults imaginary to 0
num = complex(5)
print(num)

# In case if you pass only real number, defaults imaginary to 0
num = complex(-2)
print(num)

# In case if you pass only float number, defaults imaginary to 0
num = complex(5.6,4)
print(num)

# if string is passed, it will be interpreted as complex number
num = complex('8')
print(num)

# if string is passed, it will be interpreted as complex number
num = complex('1+2j')
print(num)``````

Output

``````(5+3j)
(5-4j)
0j
(5+0j)
(-2+0j)
(5.6+4j)
(8+0j)
(1+2j)``````

## Example 2: TypeError while converting string with imaginary parameter

If the first parameter is passed as a string, then it will be interpreted as a complex number. In this case, the second parameter should be omitted. Otherwise, you get the TypeError: complex() can’t take second arg if first is a string

``````# Code to illustrate complex number

# if string is passed with imaginary number
num = complex('8',5)
print(num)
``````

Output

``````Traceback (most recent call last):
File "c:\Projects\Tryouts\listindexerror.py", line 4, in <module>
num = complex('8',5)
TypeError: complex() can't take second arg if first is a string``````

## Example 3: ValueError if string operator has a space

In the case of string conversion, if there is any space around the operator, Python will raise ValueError: complex() arg is a malformed string

``````# if you give a space in the string
num = complex('1 + 2j')
print(num)``````

Output

``````Traceback (most recent call last):
File "c:\Projects\Tryouts\listindexerror.py", line 4, in <module>
num = complex('1 + 2j')
ValueError: complex() arg is a malformed string``````

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

##### You May Also Like ## AttributeError: ‘numpy.ndarray’ object has no attribute ‘index’

Table of Contents Hide What is AttributeError: ‘numpy.ndarray’ object has no attribute ‘index’?How to fix AttributeError: ‘numpy.ndarray’ object has no attribute ‘index’?Example 1: Get the index position of an element… ## Python – List Files in a Directory

Table of Contents Hide Method 1: Using os.listdir() methodMethod 2: Using os.walk() methodMethod 3: Using os.scan() methodMethod 4: Using glob module There are several modules available in Python to list… ## Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.

Table of Contents Hide How to solve Python was not found; run without arguments to install from the Microsoft StoreScenario 1: Python is not installedScenario 2: Environment variables are not… ## Python String capitalize()

Table of Contents Hide SyntaxParameterReturn ValueExample: Capitalize a string in Python Python string capitalize() method will convert the first letter in a string to uppercase and keep the rest of the… ## Python String isspace()

Table of Contents Hide What are whitespace characters in Python?SyntaxParametersReturn valueExample 1: Working with isspace() method Example 2: Program to check if the string has only whitespace characters Python string… ## How to check if a file exists in Python?

Table of Contents Hide Python check if a file exists using OS ModuleUsing os.path.exists()Using os.path.isfile()Using os.path.isdir()Using pathlib.Path.exists() When you perform a file operation such as reading from a file or…