ValueError: could not convert string to float (Fix)

This error happens when you call float() with text that is not a valid number.

It often appears when you read input from a user, a file, a CSV, or an API. The fix is usually to inspect the string, clean it if needed, and handle bad input safely.

Quick fix

text = input("Enter a number: ").strip()
text = text.replace(",", "")

try:
    number = float(text)
    print(number)
except ValueError:
    print("Please enter a valid number, such as 3.14 or 10")

Use strip() to remove extra spaces, clean the input if needed, and catch ValueError when the text is not a valid decimal number.

What this error means

Python raises this error when float() receives a string that does not match a valid floating-point number.

float() can convert strings like:

  • '3.14'
  • '10'
  • ' 5.0 '

But it cannot convert strings like:

  • 'hello'
  • '3,14'
  • ''

If you need a basic explanation of how conversion works, see the Python float() function explained.

When this error happens

This error commonly appears when you are:

  • Converting user input from input()
  • Reading numbers from a file or CSV
  • Parsing text from an API or web page
  • Trying to convert strings that contain symbols, commas, or words

In short, the problem is usually not float() itself. The problem is the string you passed into it.

Example that causes the error

Here is a minimal example:

value = "abc"
number = float(value)

Output:

ValueError: could not convert string to float: 'abc'

This fails because "abc" is text, not a valid number format.

Common causes

These are some of the most common reasons this error happens:

  • Letters inside the string, such as '12a'
  • Empty strings, such as ''
  • Commas used as separators, such as '1,234' or '3,14'
  • Currency symbols, such as '$19.99'
  • Trailing text, such as '4.2kg'
  • Input with only spaces

Other common cases include:

  • Calling float() on text like 'hello'
  • Trying to convert an empty string
  • Including symbols like '$' or '%'
  • Reading file data with hidden newline characters or extra text
  • Assuming all user input is numeric

How to fix it

The best fix depends on where the string comes from.

In general:

  • Check the original string before converting it
  • Use strip() to remove leading and trailing spaces
  • Remove formatting characters only if you expect them
  • Use try-except to handle invalid input safely
  • Validate user input before calling float()

If your goal is general conversion, see how to convert string to float in Python.

Fix option 1: Clean the string first

This is useful when the number may include known formatting characters such as spaces, commas, or a currency symbol.

Only remove characters you are sure should not be there.

value = " $1,234.50 \n"
cleaned = value.strip().replace("$", "").replace(",", "")
number = float(cleaned)

print(number)

Output:

1234.5

What this code does:

  • strip() removes spaces and newline characters at the start and end
  • replace("$", "") removes the dollar sign
  • replace(",", "") removes the thousands separator
  • float(cleaned) converts the cleaned string

Be careful with values like '3,14'. In normal Python usage, float() expects a dot for decimals, not a comma.

Fix option 2: Use try-except

This is the safest choice when the input may be invalid.

It prevents your program from crashing and lets you show a helpful message.

value = input("Enter a price: ").strip()

try:
    price = float(value)
    print("You entered:", price)
except ValueError:
    print("That is not a valid number.")

This approach is especially useful for user input, files, and external data.

If you often work with user-entered values, see how to convert user input to numbers in Python.

Fix option 3: Check for empty input

An empty string cannot be converted with float().

Handle blank input before conversion:

value = input("Enter a number: ").strip()

if value == "":
    print("Input cannot be empty.")
else:
    number = float(value)
    print(number)

This is useful in:

  • Forms
  • Command-line tools
  • User prompts

It also helps with strings that contain only spaces, because strip() turns them into ''.

Debugging steps

If you are not sure why the conversion fails, inspect the value before calling float().

Useful debugging commands:

print(value)
print(repr(value))
print(type(value))

cleaned = value.strip()
print(repr(cleaned))

What to check:

  • Print the value before float() to see what it really contains
  • Use repr(value) to reveal hidden spaces or newline characters
  • Check the exact source of the string: user input, file, API, or calculation
  • Test with a known valid value like '3.14'

Example:

value = "12.5\n"

print(value)
print(repr(value))

number = float(value)
print(number)

Output:

12.5

'12.5\n'
12.5

In this case, the newline does not cause a problem. But hidden characters plus extra text often do.

Sometimes a similar error has a different cause.

FAQ

Can float() convert an integer string like '10'?

Yes. float('10') works and returns 10.0.

Why does float('3,14') fail?

Because float() expects a dot as the decimal separator in normal Python usage, not a comma.

Does float() ignore spaces?

Leading and trailing spaces are usually fine, but strings with only spaces still fail after stripping.

Should I always use try-except with float()?

Use it when the input may be invalid, especially with user input, files, or external data.

See also