Python TypeError must be str not int

Python TypeError must be str not int

print(the furnace is now + str(temperature) + degrees!)

cast it to str

Python comes with numerous ways of formatting strings:

New style .format(), which supports a rich formatting mini-language:

>>> temperature = 10
>>> print(the furnace is now {} degrees!.format(temperature))
the furnace is now 10 degrees!

Old style % format specifier:

>>> print(the furnace is now %d degrees! % temperature)
the furnace is now 10 degrees!

In Py 3.6 using the new f format strings:

>>> print(fthe furnace is now {temperature} degrees!)
the furnace is now 10 degrees!

Or using print()s default separator:

>>> print(the furnace is now, temperature, degrees!)
the furnace is now 10 degrees!

And least effectively, construct a new string by casting it to a str() and concatenating:

>>> print(the furnace is now  + str(temperature) +  degrees!)
the furnace is now 10 degrees!

Or join()ing it:

>>> print( .join([the furnace is now, str(temperature), degrees!]))
the furnace is now 10 degrees!

Python TypeError must be str not int

you need to cast int to str before concatenating. for that use str(temperature). Or you can print the same output using , if you dont want to convert like this.

print(the furnace is now,temperature , degrees!)

Leave a Reply

Your email address will not be published. Required fields are marked *