How to Convert a String to a Double in Python

In this tutorial, we will learn how to convert a string into a float with double precision in Python.

 

Using float()

One way of creating a float from a string is to use the Python float() function. Pass the string to convert as the first argument of the function and store the result in a variable.

 

num = float('3.55')

print(num)
3.55

 

Using decimal.Decimal()

Another way to convert a string to a float is to use the Python decimal.Decimal() function. Pass the string as the first argument of the function and store the result in a variable.

 

num = decimal.Decimal('3.55')

print(num)
3.55