Python Forum
[split] Problem with integers and strings
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] Problem with integers and strings
#1
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
Reply
#2
it means you are adding a str to an int. 1 + 'F' will always error
print(str(1) + 'F') # this will work
99 percent of computer problems exists between chair and keyboard.
Reply
#3
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
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 696 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  problem in using int() with a list of strings akbarza 4 647 Jul-19-2023, 06:46 PM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,702 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Reading integers from a file; the problem may be the newline characters JRWoodwardMSW 2 1,918 Jul-14-2020, 02:27 AM
Last Post: bowlofred
  Problem with replacing strings donnertrud 3 2,076 May-22-2020, 04:06 PM
Last Post: donnertrud
  [split] Python Class Problem astral_travel 12 4,823 Apr-29-2020, 07:13 PM
Last Post: michael1789
  Split a long string into other strings with no delimiters/characters krewlaz 4 2,703 Nov-15-2019, 02:48 PM
Last Post: ichabod801
  problem with for loop using integers python_germ 5 2,942 Aug-31-2019, 11:42 AM
Last Post: jefsummers
  re.split multiple delimiters problem gw1500se 2 3,565 Jun-24-2019, 02:43 PM
Last Post: gw1500se
  Finding multiple strings between the two same strings Slither 1 2,479 Jun-05-2019, 09:02 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020