Typeerror unsupported operand type s for str and float

In this article, we’ll walk you through how to fix this “typeerror unsupported operand type s for str and float.”

This error is quite frustrating when you don’t know how to resolve it.

Fortunately, this article will assist you. Continue reading as we will hand you the different solutions you may use.

But before we dive into the solutions, we need to understand first what this error means and why it occurs in your Python script.

What is “typeerror unsupported operand type s for str and float”?

The “typeerror unsupported operand type s for str and float” usually occurs when you try to perform an arithmetic operation between the string and float.

In addition to that, the error message indicates that you are trying to use a mathematical operator (such as +, -, *, or /) between a string and a float (a decimal number).

Unfortunately, these two data types cannot be used together in mathematical operations.

For example:

income = 150000.00
profit = 10000.00

print("Your total income this year is " + salary + bonus + ".")

As a result, it will throw an error message.

Take note:

In Python, it is not allowed to perform arithmetic operations between two different data types.

How to fix “typeerror unsupported operand type s for str and float”?

To fix this error, you have to convert one of the operands to the same data type as the other operand before performing the operation.

And ensure that you are using a data type that is compatible with your operator.

Here are the following solutions you can use to fix this error:

Solution 1: Convert float to string

We converted the sales + profit sum to a string using the str() function before concatenating it with the other strings.

sales = 150000
profit = 100000

print("Your total income this year is $" + str(sales + profit) + ".00")

Output:

Your total income this year is $250000.00

Solution 2: Convert string to float

Here, we convert the available_quantity string to a float using the float() function before performing the multiplication, which will output the correct result.

available_quantity = "100"
cost = 259.99

print("The total income is $" + str(float(available_quantity) * cost) + "0" )

Output:

The total income is $25999.00

Solution 3: Use arithmetic operation separately

Here we use the arithmetic operation separately.

We convert the salary string to an integer using the int() function before performing the addition operation with the bonus variable.

salary = "300000"
bonus = 200000

print(int(salary) + bonus)

Output:

500000

Solution 4: Use string formatting

In this solution, we use string formatting to insert the variables into the string in a formatted way.

The %s and %f placeholders are replaced by the product and price variables, respectively.

The .2 after the % sign specifies that the price variable should be formatted to two decimal places.

Product = "Chocolate"
Price = 10.75

print("The price of a %s is $%.2f." % (Product, Price))

Output:

The price of a Chocolate is $10.75.

Solutions 5: Use f-strings

Alternatively, you can use f-strings. This feature was introduced in Python 3.6.

It is used to insert the variables into the string in a formatted way.

The f before the string allows us to use curly braces {} to insert the variables directly into the string.

The “.2f” after the “Price” variable specifies that it should be formatted to two decimal places.

Product = "Chocolate"
Price = 10.75

print(f"The price of a {Product} is ${Price:.2f}.")

Output:

The price of a Chocolate is $10.75.

Conclusion

In conclusion, the “typeerror unsupported operand type s for str and float” usually occurs when you try to perform an arithmetic operation between the string and float.

Luckily, this article provided several solutions above so that you can fix the “unsupported operand type s for str and float” error message.

We are hoping that this article provided you with sufficient solutions to get rid of the error.

You could also check out other “typeerror” articles that may help you in the future if you encounter them.

Thank you very much for reading to the end of this article.

Understanding “unsupported operand type” TypeErrors

Every operator (+, -, *, /) is defined on specific type pairs. Python does NOT auto-convert types like JavaScript does. Adding an integer to a string, or comparing a list to a dict, raises TypeError.

Common triggers

  • int + str. "Age: " + 25 fails. Use f-string: f"Age: {25}".
  • str * float. "ab" * 2.5 fails. Only int multiplication is allowed on strings.
  • None arithmetic. Any arithmetic on None gives an unsupported-operand error.
  • Comparing dict to list. Python 3 raises TypeError on many cross-type comparisons (unlike Python 2).
  • Path + str. pathlib.Path + string works only with / (division operator), not +.

Diagnostic pattern

# BAD — mixing int and str
count = 5
msg = "You have " + count + " items"
# TypeError: can only concatenate str (not "int") to str

# GOOD — three modern ways
msg = f"You have {count} items"       # f-string (Python 3.6+)
msg = "You have {} items".format(count)  # .format()
msg = "You have " + str(count) + " items"  # explicit conversion

Best practices

  • Prefer f-strings. Cleaner and faster than concatenation or .format().
  • Use str() explicitly when you need a string representation of any non-string value.
  • Use Decimal for money. Mixing float and Decimal in arithmetic often causes surprises.

Frequently Asked Questions

What is Python TypeError and what causes it?

TypeError is raised when an operation is applied to an object of the wrong type. Common patterns: calling a non-callable object, adding incompatible types (str + int), passing the wrong number of arguments, or accessing attributes on a NoneType. Each TypeError message names the operation and expected vs actual types, the fix is almost always to convert types explicitly (int(), str()) or fix the wrong variable assignment.

How do I quickly debug a Python TypeError?

Three steps: (1) Read the full error message, it names the exact operation and types involved. (2) Print the type of every variable in that line: print(type(var1), type(var2)). (3) Check what the function expected vs what you passed. Most TypeError fixes are 1-line type casts or fixing a variable that became None unexpectedly.

Should I catch TypeError or let it propagate?

For internal code, let TypeError propagate, it’s almost always a real bug (wrong type passed). For boundary code (parsing user input, third-party API responses), catch TypeError + ValueError together: try: parsed = int(value) except (TypeError, ValueError): parsed = 0. Catching internal TypeErrors hides bugs.

How do I prevent TypeError in production?

Three patterns: (1) Use type hints (def add(a: int, b: int) -> int) and check with mypy / pyright in CI. (2) Validate inputs at boundaries (Pydantic for FastAPI, DRF serializers for Django). (3) Default values that match expected types (return 0 not None for numeric functions). Static typing catches 80% of TypeErrors before runtime.

Where can I find more TypeError fixes?

Browse the TypeError reference hub for 220+ specific TypeError fixes. For broader Python debugging, see the Python Tutorial hub. For related error types, see ValueError and AttributeError guides.

Caren Bautista


Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel
 · View all posts by Caren Bautista →