How to Convert String to Double in Python?

In Python, the “string” values are represented by a sequence of characters/alphabet or numbers enclosed in single/double-quotes. The string value is necessary to convert into a number in certain situations. The purpose may be to perform any numeric/mathematical operation on them.

To convert the string into numbers, various functions are used in Python, such as float() and “Decimal()”, etc.

This post will cover the possible methods to convert a string into a double in Python with the following outline:

Before getting into the core part of this post, first understand the concept of Double and Float in Python.

Are Double and Float the Same in Python?

The “float” data type is used in Python instead of “double” to represent numerical values with decimal points. In Python, the “float” and “double” data types are not different. All floating point numbers are represented by “float” in Python.

Let’s look at possible methods to convert a Python string to double.

Method 1: Using float() Function

The “float()” function is utilized in Python to convert the “string” into “float”. Let’s convert the string into double using the “float()” function:

Code:

string_val = "9.025657453"
float_value = float(string_val)
print(float_value)
print(type(float_value))

In the above code, the “float()” function accepts the numeric string as an argument and returns the float value /Double value.

Output:

The above output shows that the string has been converted into double using the “float()” function.

Method 2: Using Decimal() Function

The “decimal.Decimal()” class is the subclass of “float()” and can be accessed by importing the “decimal” module at the start. This function provides accurate rounded decimal arithmetic values with high precision. Here is an example:

Code:

from decimal import Decimal
string_val = "1994.025657453"
decimal_value = Decimal(string_val)
print(decimal_value)
print(type(decimal_value))

In the above code, “Decimal()” function is used to convert the numeric string into decimal values.

Output:

The numeric string has been successfully converted into double using the “Decimal()” function.

Note: if the string value is not the actual representation of the number, then the “ValueError” will appear in the program. Let’s have a look at the below code:

Code:

string_val = "Alex"
float_value = float(string_val)
print(float_value)
print(type(float_value))

In the above code, the string “Alex” has been converted into “float” using the “float()” function which will cause an error.

Output:

The above output verified that the “float()” function only converts the numeric value into float. If the value is not numeric the above error will appear in a program.

That’s how the string values can be converted to double in Python.

Conclusion

To convert the string to double, the “float()” function and the “Decimal()” function of the “decimal” module is used in Python. The “float()” function accepts the string as a parameter and returns the float/double value. Similarly, the “Decimal()” function of the decimal module is used when we require a higher level of precision and wider range. This post has presented a detailed guide on converting the string to double in Python.