. Advertisement .
..3..
. Advertisement .
..4..
Python is not language developers aren’t aware of, in fact, it is a programming language almost all developers are working on, be it their project or any clients. This language comprises different functions that also involve operators and operands. While working, typeerror: unsupported operand type(s) for /: ‘str’ and ‘int’ is an alarming error that occurs, and developers often look for the correct answer to keep the functions working.
TypeError: unsupported operand type(s) for /: 'int' and 'str'
Let’s dig more to find out what makes this error occurs and how to solve it
What causes the “typeerror: unsupported operand type(s) for /: ‘str’ and ‘int’” error?
The prime cause of typeerror: unsupported operand type(s) for /: ‘str’ and ‘int’ is the division operator “/
” when we use it with a number and a string. Whenever the data of any object involves in an operation is inappropriate, typeerror occurs.
Dividing a string and an integer when the data type of object is not compatible, the interpreter of Python shows up the Typeerror.
Let’s take a look at the example
a = 99
b = "11"
c = a / b
Output:
Traceback (most recent call last): File "/home/aditya1117/PycharmProjects/pythonProject/string12.py", line 3, in <module>
c = a / b
TypeError: unsupported operand type(s) for /: 'int' and 'str'
How to Solve?
Whenever a division operator is used, it divides the operands, the first one by the second, and then returns the value, which is a float. Take a look at another example with a solution
Using a function int ()
Example
a = 99
b = "11"
print(f'a / b = {a / b}')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
print(f'a / b = {a / b}')
TypeError: unsupported operand type(s) for /: 'int' and 'str'
Solution
The easiest and quickest solution is to convert the variable y to an integer by using a function int (). Once you edit, the code will work fine. Let’s check out the revised code
a = 99
b = "11"
print(f'a / b = {a / int(b)}')
Output:
a / b = 9.0
Hence, the division or typeerror: unsupported operand type(s) for /: ‘str’ and ‘int’ is solved.
Use float () to convert strings
Example
Let’s take a look at a bit complex program to understand the error and solution that includes division as well as multiplication and addition operators
Take input from the user and calculate the area of a triangle
edge1 = input('Enter the first edge: ')
edge2 = input('Enter the second edge: ')
edge3 = input('Enter the third edge: ')
# calculate the semi-perimeter
p = (edge1 + edge2 + edge3) / 2
# calculate the area
area = (p*(p-edge1)*(p-edge2)*(p-edge3)) ** 0.5
print('The area of the triangle equal ' + str(area))
Output:
Enter the first edge: 9
Enter the second edge: 9
Enter the third edge: 9
Traceback (most recent call last):
File "<string>", line 6, in <module>
TypeError: unsupported operand type(s) for /: 'str' and 'int'
Solution
In Python, one can’t multiply a string value with a non-integer value. You get an error if multiply a string with a string without converting it to a float or integer.
The possible solution to fix this error is to use float ()
to convert strings. Check out the solution code
edge1 = float(input('Enter the first edge: '))
edge2 = float(input('Enter the second edge: '))
edge3 = float(input('Enter the third edge: '))
# calculate the semi-perimeter
p = (edge1 + edge2 + edge3) / 2
# calculate the area
area = (p*(p-edge1)*(p-edge2)*(p-edge3)) ** 0.5
print('The area of the triangle equal ' + str(area))
Output:
Enter the first edge: 9
Enter the second edge: 9
Enter the third edge: 9
The area of the triangle equals 35.074028853269766
Conclusion
And there you know how to convert strings to integers or float!
Python programming is fun if you know the trick to solve the typeerrors. I hope with the help of the examples and solutions to convert the data type, you will crack the right code in your next programming.
With this, I wish you happy coding!
Feel free to drop a comment if you encounter any other programming issues.
Read more:
→ How To Deal With TypeError: Unsupported Operand Type(s) For +: ‘Int’ and ‘Str’
Leave a comment