The python programming language includes a wide range of operators capable of manipulating the operands. The expression is generated using the operators and the operands. There is a need to work together with operators on different types of operands. The error ‘TypeError: can only concatenate str (not “float”) to str‘ is due to improper use of the various operands with python operators.
In this post, we’ll see what this error “TypeError: can only concatenate str (not “float”) to str” is about, How to fix this error. The various solutions to solve this error are set out in this article.
Traceback (most recent call last):
File "D:\pythonexample.py", line 3, in <module>
print ("power" + p)
TypeError: can only concatenate str (not "float") to str
>>>
Root Cause
Operators are used to operate with two identical data type operands. Two same data type variables are used to perform any operation of the python program. Python will not allow any manipulation if there are different types of data. The error shown above is due to this reason.
Python supports work around to manipulate different variables of data type using built-in functions. These built-in functions will help to convert from one data type to another. Error indicates that the program is concatenating a string with a non-string variable. Error will be fixed if we convert a non-string variable to a string variable.
How to reproduce this issue
Create two variables with two different data types in the python program. Add these variables and print them in the console. The Python compiler can not add these two variables because they are both different data types. This is why the compiler will throw this error saying that the two variables can not be concatenated.
import math
p = math.pow(3,1)
print ("power" + p)
Output
Traceback (most recent call last):
File "D:\pythonexample.py", line 3, in <module>
print ("power" + p)
TypeError: can only concatenate str (not "float") to str
>>>
Solution 1
The above program is trying to add a string and a number. Since python does not allow a string and a number to be added, both should be converted to the same data type. The python function str() is used to convert a number to a string. Use the str() function to convert the number to the program.
import math
p = math.pow(3,1)
print ("power" + str(p))
Output
power 3.0
>>>
Solution 2
If two variables are added to the print statement, the print statement allows more than one parameter to be passed. Set the string statement and the number as two parameters in the print statement. The print statement internally converts all values to the string statement. The code below shows how to use your print statement.
import math
p = math.pow(3,1)
print ("power" , p)
Output
power 3.0
>>>
Solution 3
The Python program allows you to create a string by dynamically passing arguments. Create a string with a place holder. In the run-time, python replaces the place holder with the actual value.
import math
p = math.pow(3,1)
msg = f'power {p} '
print (msg)
Output
power 3.0
>>>