How to Convert a String to a Double in Python?

Problem Formulation

  • Given a string containing digits such as '123.456'.
  • How to convert it to a double in Python such as 123.456?

Solution

Python doesn’t provide an explicit “double” data type. However, it provides the float type that behaves the same and has the same precision as doubles in C, C++, or Java. To convert a string with digits to a “double” (i.e., “float”) in Python, use the built-in function float(string). For example, the expression float('123.456') will yield the float result 123.456.

>>> float('123.456')
123.456
>>> float('0.00000001')
1e-08
>>> float('10.999999')
10.999999

You can learn more about this conversion function in the following video: