[split] Problem with integers and strings - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: [split] Problem with integers and strings (/thread-7340.html) |
[split] Problem with integers and strings - aka2d7 - Jan-04-2018 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 RE: [split] Problem with integers and strings - Windspar - Jan-05-2018 it means you are adding a str to an int. 1 + 'F' will always error print(str(1) + 'F') # this will work RE: [split] Problem with integers and strings - aka2d7 - Jan-05-2018 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 RE: [split] Problem with integers and strings - snippsat - Jan-05-2018 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 FEven 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 |