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 13, 2019 in Python by Tina

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

3 answers 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 data type into int(or float)

print(int(b)-int(a))
If you need to know more about Python, It's recommended to join Python course today.
answered May 13, 2019 by Anjali
thank you, i had the same concern and you helped me ;)
0 votes

By turning them into integers instead:

percent = (int(pyc) / int(tpy)) * 100;

In python 3, the input() function returns a string. Always. This is a change from Python 2; the raw_input() function was renamed to input().

answered Dec 11, 2020 by Gitika
• 65,910 points
0 votes

& is "bitwise and" operand in Python, you should use and instead

from wiki.python.org:

x & y : Does a "bitwise and". Each bit of the output is 1 if the corresponding bit of x AND of y is 1, otherwise it's 0.

"bitwise and" works like this:

>>> 1 & 0
0
>>> 0 & 0
0
>>> 1 & 1
1
answered Dec 11, 2020 by Rajiv
• 8,910 points

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
11,698 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, 2020 in Python by Roshni
• 10,520 points
3,273 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, 2020 in Python by anonymous
• 220 points

edited May 18, 2020 by Gitika 2,465 views
0 votes
0 answers

TypeError: unsupported operand type(s) for *: 'CommentedMap' and 'CommentedMap' Hatam bu nasıl çözebilirim ?

from ruamel import yaml from scipy import spatial import ...READ MORE

Sep 29, 2020 in Python by anonymous
• 120 points

edited Sep 29, 2020 by Gitika 643 views
0 votes
0 answers

How can I solve it? TypeError: unsupported operand type(s) for /: 'float' and 'list'

I am trying to calculate numerical integration ...READ MORE

Nov 17, 2020 in Python by asimsek
• 120 points
13,091 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

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

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
3,487 views
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 13, 2019 in Python by Dinesh
2,693 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP