close

[Solved] TypeError: unsupported operand type(s) for -: ‘str’ and ‘int’

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error TypeError: unsupported operand type(s) for -: ‘str’ and ‘int’ in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How TypeError: unsupported operand type(s) for -: ‘str’ and ‘int’ Error Occurs?

Today I get the following error TypeError: unsupported operand type(s) for -: ‘str’ and ‘int’ in python.

How To Solve TypeError: unsupported operand type(s) for -: ‘str’ and ‘int’ Error ?

  1. How To Solve TypeError: unsupported operand type(s) for -: 'str' and 'int' Error ?

    To Solve TypeError: unsupported operand type(s) for -: 'str' and 'int' Error For future reference Python is strongly typed. Unlike other dynamic languages, it will not automagically cast objects from one type or the other (say from str to int) so you must do this yourself.

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

    To Solve TypeError: unsupported operand type(s) for -: 'str' and 'int' Error For future reference Python is strongly typed. Unlike other dynamic languages, it will not automagically cast objects from one type or the other (say from str to int) so you must do this yourself.

Solution 1

  1. The reason this is failing is because (Python 3) input returns a string. To convert it to an integer, use int(some_string).
  2. You do not typically keep track of indices manually in Python. A better way to implement such a function would bedef cat_n_times(s, n): for i in range(n): print(s) text = input("What would you like the computer to repeat back to you: ") num = int(input("How many times: ")) # Convert to an int immediately. cat_n_times(text, num)
  3. I changed your API above a bit. It seems to me that n should be the number of times and s should be the string.

Solution 2

For future reference Python is strongly typed. Unlike other dynamic languages, it will not automagically cast objects from one type or the other (say from str to int) so you must do this yourself. You’ll like that in the long-run, trust me!

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read

Leave a Comment