Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk3 min

How to Fix Python’s “Could Not Convert String to Float” Error

Python’s float() error means the string does not match its numeric format. Identify the input and apply the right fix without silently changing its value.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Python raises ValueError when float() receives a string whose contents do not match its numeric format. Inspect the exact value first, then choose a fix that matches the input: remove known decoration, parse the correct locale, handle invalid rows deliberately, or use a decimal type when needed.

Why float() raises this error

The argument can be a string and still contain an invalid value. Python’s float() accepts numeric text such as signed decimals and exponent notation, along with surrounding whitespace and spellings for infinity and NaN. It does not accept arbitrary words, currency symbols, or punctuation that conflicts with the numeric syntax. See the Python 3.14.7 float() reference and the Python 3.12.15 definition of ValueError.

As an Amazon Associate I earn from qualifying purchases.

For example, float(" 12.5 ") works because surrounding whitespace is allowed. But float("$12.50") and float("12,50") fail: the first includes a currency mark, and the second may use a decimal convention that Python does not infer.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Five fixes for the error

1. Inspect the exact string

Print the value with repr() immediately before conversion. It reveals characters that ordinary printing can hide, such as tabs and newlines.

print(repr(value))
number = float(value)

If conversion fails, identify which input record produced the value and check the upstream source. Catching the exception without recording or examining the bad value can conceal a data-quality problem rather than fix it.

2. Remove only known decoration

Python already accepts whitespace at the beginning and end of a numeric string, so trimming alone will not remove a currency symbol, label, or other unexpected character. If the input format guarantees a particular decoration, remove that specific text before parsing.

value = "$12.50"
cleaned = value.removeprefix("$")
number = float(cleaned)

Do not strip punctuation indiscriminately. Deleting every comma, for example, can turn a decimal value into a different number. Normalize only characters whose meaning is established by the source format.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

3. Parse separators using the source’s locale

Grouping and decimal separators vary. For instance, 1,234.50 and 1.234,50 represent the same quantity under different conventions. Decide which convention the source uses before parsing; punctuation removal without that knowledge can silently corrupt values.

For locale-defined input, configure the intended numeric locale and use locale.atof(), which interprets separators according to that locale. The setting must match the data source. Python documents this behavior in its locale conversion reference.

import locale

# Configure the intended LC_NUMERIC in the application first.
number = locale.atof("1.234,50")

4. Parse a pandas column deliberately

For a Series or other one-dimensional collection, pandas.to_numeric() raises on invalid values by default. If the workflow should retain valid entries while marking invalid ones, use errors="coerce"; those invalid entries become NaN, so inspect and report them rather than treating them as valid data.

import pandas as pd

values = pd.Series(["1.5", "not available", "2.0"])
parsed = pd.to_numeric(values, errors="coerce")
bad_rows = values[parsed.isna()]
print(bad_rows)

Choose coercion only when a missing-value marker is appropriate for the application. The pandas 3.0.6 API reference also cautions that very large values may lose precision in array-backed numeric storage.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

5. Use Decimal when decimal arithmetic matters

If the domain requires decimal representation, such as calculations where decimal rounding matters, parse a valid decimal string with Decimal instead of converting it to a binary floating-point number.

from decimal import Decimal

amount = Decimal("12.50")

Decimal has its own accepted string syntax; it is not a general parser for currency-formatted text. Remove or interpret formatting according to the input’s known rules before constructing the value. See the Python 3.14.8 Decimal documentation.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Which fix should you choose?

Situation Approach Important check
You do not know what the failing value contains Inspect it with repr() Trace the value to its source record
The input has a known symbol or label Remove that exact decoration, then call float() Do not remove punctuation with unknown meaning
Numbers use locale-specific separators Use the matching locale with locale.atof(), or a validated format-specific normalization Confirm the source convention and locale
You are parsing a pandas column and want invalid entries marked missing Use pd.to_numeric(..., errors="coerce") Find and review the resulting NaN rows
Decimal representation is required Use Decimal with valid decimal text It does not automatically parse formatted currency

Do not use eval() as a conversion shortcut

eval() is not a safe way to turn text into a number: Python’s FAQ notes that it is slower and creates a security risk. Use a numeric parser suited to the actual input format instead. See the Python 3.14.7 numeric-conversion FAQ.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

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

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.