Convert Python string to double | Example code

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

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))

Do comment if you have any doubts and suggestions on this Python string double program.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

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


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.