How do I convert a string to a double in Python?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Python, the usual equivalent of a double-precision number is float. Converting a string is therefore usually just float(text), but real code also has to think about invalid input, formatting noise, and whether binary floating point is appropriate for the domain.
Basic Conversion With float
For clean numeric input, use float directly.
It also handles scientific notation.
If the string is trusted and uses a normal dot decimal separator, that is all you need.
Handle Invalid Input Explicitly
When strings come from users, files, or APIs, wrap the conversion in try and except.
That gives the caller a useful error instead of a silent failure.
Clean Formatting Noise First
float does not understand commas, currency symbols, or unit suffixes automatically. If the input is formatted, clean it before conversion.
Be careful with this approach if the input may use locale-specific conventions, because not every comma means “thousands separator.”
Watch for nan and inf
Python accepts some special floating-point strings.
These values can move through calculations silently. If your domain requires only finite numbers, validate after parsing.
Use Decimal When Precision Matters
If the question really means “a precise decimal number” rather than “Python float,” then Decimal may be the better tool.
float uses binary floating point, which is excellent for many scientific and engineering tasks but can introduce surprising rounding artifacts in financial calculations.
Converting Many Strings at Once
For batch processing, collect both successful conversions and failures instead of crashing on the first bad value.
That pattern is useful in ingestion pipelines where partial success is acceptable and bad rows need review.
Convert Entire Columns With a Policy
In real applications, string-to-float conversion often happens at the boundary of a CSV import, API payload, or analytics job. It helps to decide early whether invalid values should fail fast, be logged and skipped, or become missing values for later review.
That policy choice matters because conversion is not only about syntax. It is also about how your system handles bad data once it arrives.
Common Pitfalls
The biggest mistake is assuming every numeric-looking string will work with float. Formatted values with commas, units, or unexpected separators often fail.
Another issue is swallowing ValueError and defaulting silently to zero, which hides upstream data problems.
A third problem is using float for money when exact decimal behavior matters more than speed.
Summary
- Use
float(text)for ordinary numeric strings in Python. - Strip whitespace and catch
ValueErrorfor robust parsing. - Clean formatting noise before conversion when necessary.
- Validate
nanandinfif only finite values are allowed. - Use
Decimalinstead offloatwhen exact base-10 arithmetic matters.
Related reading
- How do I convert all of the items in a list to floats?
- How do I convert all strings in a list of lists to integers?
- How do I convert this list of dictionaries to a csv file?
- How do I copy an entire directory of files into an existing directory using Python?
- How do I correctly clean up a Python object?
- How do I correctly setup and teardown for my pytest class with tests?
- How do I count occurrence of unique values inside a list
- How do I count the NaN values in a column in pandas DataFrame?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.