- Given a string containing digits such as
- How to convert it to a double in Python such as
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
>>> 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:
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.