How To Print Complex Numbers In Python

When working with Python, you might come across situations where you need to perform calculations with complex numbers.

A complex number is a number of the form a + bi, where a and b are real numbers, and i represents the imaginary unit, with the property i^2 = -1. In this tutorial, we will learn how to print complex numbers in Python, as well as some basic operations to deal with them.

Step 1: Representing Complex Numbers in Python

Python provides a built-in data type called complex to represent complex numbers. You can create a complex number by using the complex() function or by directly writing the number in the form of a + bj, where ‘a’ and ‘b’ are real numbers, and ‘j’ is the imaginary unit.



Step 2: Accessing Real and Imaginary Parts

When you have a complex number, you can access its real and imaginary parts using the real and imag attributes, respectively.


 2.0 3.0

Step 3: Basic Operations with Complex Numbers

Python allows you to perform basic arithmetic operations with complex numbers, such as addition, subtraction, multiplication, and division.



Step 4: Formatting Complex Numbers for Printing

To print complex numbers in a more readable format, you can use formatted string literals or str.format() method.

Using formatted string literals (f-strings):


 The complex number is 2.0 + 3.0j

Using str.format():


 The complex number is 2.0 + 3.0j

Full Code


Now you know how to represent, print, and perform basic operations with complex numbers in Python. Remember to use the complex data type and the handy real and imaginary attributes to make working with complex numbers easier.