Python Forum

Full Version: [split] Problem with integers and strings
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Please help!
I very new to python and need to understand why I'm getting this error message for this output.
Traceback (most recent call last):
File "/Users/aka2d7/Documents/Output ex.py", line 24, in <module>
print(city, "is currently", description, "and temperature is", temperature + "F")
TypeError: unsupported operand type(s) for +: 'int' and 'str'

I can run it and it will work up until the (+) plus sign and the "F". What does this error message mean?

Thanks,
aka2d7
it means you are adding a str to an int. 1 + 'F' will always error
print(str(1) + 'F') # this will work
Hi Windspar,

I really appreciate your feedback!!! It definitely worked and now I have learned something new. I'm so excited!!! Thanks soooo much!

aka2d7
A better way is to use string formatting,then there is no need to convert to string or use of ,+ everywhere.
>>> city = 'Oslo'
>>> description = 'Windy'
>>> temperature = 25
>>> print('{} is currently {} and temperature is {} F'.format(city, description, temperature))
Oslo is currently Windy and temperature is 25 F
Even better in 3.6 we got f-string.
>>> print(f'{city} is currently {description} and temperature is {temperature} F')
Oslo is currently Windy and temperature is 25 F