TypeError: unsupported operand type(s) for -: 'str' and 'str'

0 votes

Hi. I am new to Python and I am practicing to code. But I am facing some issues.

>>> a=input('>')
>3
>>> b=input('>')
>5
>>> print(b-a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'str' and 'str'
>>>

Please help me solve this error

May 12, 2019 in Python by Tina

edited Jun 25 by MD 23,124 views
Please declare the data type to your variable like int or float else python by default takes as string

1 answer to this question.

0 votes

Hi. This error is caused because you are trying to do a subtraction between two strings. When you take the input, the input data is stored as a string by default. To apply subtraction, you can change the type into int(or float)

print(int(b)-int(a))
answered May 12, 2019 by Anjali
thank you, i had the same concern and you helped me ;)

Related Questions In Python

0 votes
0 answers

TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

Hi guys i have below code . ...READ MORE

Oct 30, 2019 in Python by sumanth
• 190 points
3,779 views
0 votes
1 answer

TypeError: unsupported operand type(s) for -: 'list' and 'int'

Hey, @William For me the code is working ...READ MORE

answered Mar 11 in Python by Roshni
• 1,130 points
412 views
+1 vote
0 answers

TypeError: unsupported operand type(s) for /: 'list' and 'list'

This is my part of code, why ...READ MORE

May 17 in Python by anonymous
• 220 points

edited May 18 by Gitika 212 views
0 votes
1 answer

Python error "TypeError: Cannot compare types 'ndarray(dtype=int64)' and 'str'"

Hey @Ashish, change the emotion_map to the ...READ MORE

answered May 30, 2019 in Python by Mir
1,858 views
0 votes
1 answer
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,074 views
0 votes
0 answers
+4 votes
6 answers
0 votes
1 answer

What does the command df.describe() do for Python Pandas Dataframe?

Hi @Rajat, if you printed the output, ...READ MORE

answered May 12, 2019 in Python by Dinesh
1,289 views