Unpack String of Integers to Complex Numbers in Python



A string contains two integers separated by comma. It is first split in a list of two strings having digits.

>>> s="1,2".split(",")
>>> s
['1', '2']

Two items are then converted to integers and used as arguments for complex() function

>>> complex(int(s[0]), int(s[1]))
(1+2j)

This results in unpacking of string of integers in a complex number


Updated on: 2020-02-20T08:09:49+05:30

169 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements