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.