Skip to content

Convert Python string to double | Example code

  • by

Use the float() method or decimal() method to convert string to double in Python. The conversion of string to double is the same as the conversion of string to float.

If the string contains non-numeric characters, then the float() function will raise a ValueError exception.

Example convert a string to a double in Python

Simple example code only works on a string with a number or decimal values.

Using float() method

s = "2.367"

res = float(s)

print(res)
print(type(res))

Output:

Convert Python string to double

Using decimal() method

For this example, you have to import a decimal module.

from decimal import Decimal

s = "2.367"

res = Decimal(s)

print(res)
print(type(res))

Comment if you have any doubts or suggestions on this Python string double program.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *