-3

I have a string in this format "[12.45,56.78]" , How to extract the float numbers and assign them in two different variables . newbie in python please help

Anas.
  • 3
  • 1

2 Answers2

2

or

x = "[12.45,56.78]"
myList= eval(x)

myList[0]
Out[1]: 12.45

myList[1]
Out[2]: 56.78
Timo Kvamme
  • 2,806
  • 1
  • 19
  • 24
1

You can use ast.literal_eval() to evaluate the string and convert to list and then unpack them into two variables. Example -

>>> import ast
>>> a,b = ast.literal_eval("[12.45,56.78]")
>>> a
12.45
>>> b
56.78
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176