close

[Solved] TypeError: can only concatenate str (not “float”) to str

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error TypeError: can only concatenate str (not “float”) to str in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How TypeError: can only concatenate str (not “float”) to str Error Occurs?

Today I get the following error TypeError: can only concatenate str (not “float”) to str in python.

How To Solve TypeError: can only concatenate str (not “float”) to str Error ?

  1. How To Solve TypeError: can only concatenate str (not “float”) to str Error ?

    To Solve TypeError: can only concatenate str (not “float”) to str Error You have to assign the cast to the variable. Onlystr(peso) doesn't modify it. Because str() returns a str type. So, you need to do that:

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

    To Solve TypeError: can only concatenate str (not “float”) to str Error You have to assign the cast to the variable. Onlystr(peso) doesn't modify it. Because str() returns a str type. So, you need to do that:

Solution 1

You have some options about how to go about this

Using peso = str(peso) and same for volume = str(volume)

peso = float(input("Qual o peso do plastico da sua protese?"))
volume = float(input("Qual o volume do material?"))
peso = str(peso)
volume = str(volume)

def resultados():
  print('O peso do plastico é de ' + peso, end="", flush=True)

resultados()
print(' g e tem um volume de ' + volume + "dm^3")

Or you could just convert them to str when you are performing your print this way you can preserve the values as floats if you want to do more calculations and not have to convert them back and forth over and over

peso = float(input("Qual o peso do plastico da sua protese?"))
volume = float(input("Qual o volume do material?"))

def resultados():
  print('O peso do plastico é de ' + str(peso), end="", flush=True)

resultados()
print(' g e tem um volume de ' + str(volume) + "dm^3")

Solution 2

You have to assign the cast to the variable. Onlystr(peso) doesn’t modify it. Because str() returns a str type. So, you need to do that:

peso = str(peso)

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read