TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’ in Python

In python, “TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’ ” is shown while adding int with a python string. Python can’t add an integer with a string. TypeError will be thrown saying unsupported operant types by python interpreter.

In python, the plus “+” is used to add two numbers as well as two strings. The mathematical addition of the two numbers is for numbers. For strings, two strings are concatenated. There is a need to concatenate a number and a string in programming. The plus “+” operator can not be used for this reason. When you concatenate an integer and a string, this error is thrown.

Through this article, we can see what this error is, how this error can be solved. This type error is a mismatch of the concatenation of two different data type variables. The error would be thrown as like below.

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    print x + y
TypeError: unsupported operand type(s) for +: 'int' and 'str'
[Finished in 0.1s with exit code 1]


Different Variation of the error

TypeError: unsupported operand type(s) for +: 'int' and 'str'
TypeError: unsupported operand type(s) for -: 'int' and 'str'
TypeError: unsupported operand type(s) for /: 'int' and 'str'
TypeError: unsupported operand type(s) for %: 'int' and 'str'
TypeError: unsupported operand type(s) for //: 'int' and 'str'
TypeError: unsupported operand type(s) for ** or pow(): 'int' and 'str'
TypeError: unsupported operand type(s) for +=: 'int' and 'str'
TypeError: unsupported operand type(s) for -=: 'int' and 'str'
TypeError: unsupported operand type(s) for /=: 'int' and 'str'
TypeError: unsupported operand type(s) for %=: 'int' and 'str'
TypeError: unsupported operand type(s) for //=: 'int' and 'str'
TypeError: unsupported operand type(s) for ** or pow(): 'int' and 'str'
TypeError: unsupported operand type(s) for &=: 'int' and 'str'
TypeError: unsupported operand type(s) for |=: 'int' and 'str'
TypeError: unsupported operand type(s) for ^=: 'int' and 'str'
TypeError: unsupported operand type(s) for <<=: 'int' and 'str'
TypeError: unsupported operand type(s) for >>=: 'int' and 'str'


Root Cause

The error is due to the mismatch of the data type. Operators in python support the operation of the same data type. When an operator is used for two different data types, this type mismatch error will be thrown.

In the program, if two separate data types, such as integer and string, are used with plus “+” operators, they should first be converted to the same data type, and then an additional operation should be carried out.



How to reproduce this issue

Create two separate data type variables in python, say an integer value and a string value. Using the plus “+” operator to concatenate these values. This type error will be seen due to the mismatch between the data types of the values.

The code below indicates the concatenation of two separate data type values. The value of x is the integer of y. The value of y is a string.

x = 5
y = "Yawin Tutor"
print x + y

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    print x + y
TypeError: unsupported operand type(s) for +: 'int' and 'str'
[Finished in 0.1s with exit code 1]


Solution 1

Python cannot add a number and a string. Either both the variable to be converted to a number or a string. If the variables are changed to a number, the mathematical addition operation will be done. The error “TypeError: unsupported operand type(s) for +: ‘int’ and ‘str'” will be resolved.

x = 5
y = 8
print x + y

Output

13
[Finished in 0.1s]


Solution 2

In the above program, The variables are converted to a string. Python interpreter concatenate two variables. the variable value is joined together. The example below shows the result of addition of two strings.

x = '5'
y = '8'
print x + y

Output

58
[Finished in 0.1s]


Solution 3

The above program is trying to add a string and an integer. Since python does not allow a string and an integer to be added, both should be converted to the same data type. The python function str() is used to convert an int to a string. Use the str() function to convert the integer number to the program.

x = 5
y = "Yawin Tutor"
print str(x) + y

Output

5Yawin Tutor
[Finished in 0.1s]


Solution 4

If two variables are added to the print statement, the print statement allows more than one parameter to be passed. Set the string statement and the integer as two parameters in the print statement. The print statement internally converts all values to the string statement. The code below shows how to use your print statement.

x = 5
y = "Yawin Tutor"
print x , y

Output

5Yawin Tutor
[Finished in 0.1s]


Solution 5

The Python program allows you to create a string by dynamically passing arguments. Create a string with a place holder. In the run-time, python replaces the place holder with the actual value.

x = 5
y = "Yawin Tutor"
print "{}".format(x) + y

or

print "{} Yawin Tutor".format(x)

Output

5 Yawin Tutor
[Finished in 0.1s]



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *