Solve – TypeError: can only concatenate str (not “float”) to str

Solve – TypeError: can only concatenate str (not “float”) to str is an error which occurs in Python when you misuse str.

In this article I am going to show you why this error is happening and how you can go about solving it. I am also going to share with you other possible solutions which have worked for many devs who have suffered from this same error.

Explaining TypeError: can only concatenate str (not “float”) to str

The problem happens when you do not reassign str to a variable since str does not modify the variable.

The error message should look like the error message in the example bellow.

                                                                       #
TypeError: can only concatenate str (not "float") to str
                                                                       #

Bellow, we will describe how the error can be solved. With multiple possible solutions which can help you get rid of the issue for good.

Solution 1 : convert to str while printing

The solution is simple, let us say we have a variable that we want to print, let us call it strange.

First, we initialize it.

                                                                       #
strange = float(input("your average message"))
                                                                       #

Then we convert it to str while we are printing it, just like this.

                                                                       #
def resultados():
print(' your average message ' + str(strange), end="", flush=True)
                                                                       #

This should be enough to solve the error. If this did not work, please try using the solution bellow.

Solution 2 : reassign to a variable

If the solution above fails, let us say we have a variable that we want to print, let us call it strange.

First, we initialize it.

                                                                       #
strange = float(input("your average message"))
                                                                       #

Then, we try turning it to an str type, by doing that in the absolute worst way possible.

                                                                       #
str(strange)
                                                                       #

The solution is doing it this way.

                                                                       #
strange = str(strange)
                                                                       #

If this does not work, try the solution bellow.

Solution 3 : format the string

Another solution that worked for a lot of devs is to format the string.

                                                                       #
strvaler = "warrior"
floatvaler = 1990
output = f'Successful concatenation : {strvaler} {floatvaler}'
                                                                       #

The result is something like this.

                                                                       #
Successful concatenation : warrior 1990
                                                                       #

I hope the solutions above fixed your problem, good luck with the scripts to come.

Summing-up

Friends, I hope this article was helpful and help you in any way, shape or form, the first solution worked for me and also the third, depending on your case the solution could be different, just keep trying and do not give up, keep coding, cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/