In this article, we talk about the “TypeError: unsupported operand type(s) for +: int and str” error in Python. This type of error occurs when we try to perform a mathematical operation between an integer and a string value. We must cast the variable to a number before using the additional operator. Keep reading for detailed information.
Why does this error occur?
In Python programming, the “TypeError: unsupported operand type(s) for +: int and str” error arises when we try to use the additional operator between a variable of type ‘int’ and a variable of type ‘string’.
To illustrate, here is an example of how this error occurs:
numValue = 1986 strValue = '1986' result = numValue + strValue print(f'The sum is: {result}')
Error:
TypeError: unsupported operand type(s) for +: 'int' and 'str'
As you can see, both the ‘numValue’ variable and the ‘strValue’ variable contain the value ‘1986’. However, these two variables have two different types, ‘int’ and ‘string’.
Simply put, Python tells you that you cannot add an integer to a string using the addition operator via the above error message.
Solved – TypeError: unsupported operand type(s) for +: int and str
To work around this problem, you just need to convert the string to a number (float or integer).
You can use the int() function to cast the ‘string’ type to the ‘int’ type.
numValue = 1986 strValue = '1986' # Cast the 'strValue' to an integer result = numValue + int(strValue) print(f'The sum is: {result}')
Output:
The sum is: 3972
Or you can also use the float() function to cast the ‘string’ type to the ‘int’ type.
numValue = 1986 strValue = '1986' # Cast the 'strValue' to a float result = numValue + float(strValue) print(f'The sum is: {result}')
Output:
The sum is: 3972.0
Alternatively, you can convert all values to string using str() function, then perform string concatenation.
numValue = 1986 strValue = '1986' # Cast the 'numValue' to a string result = str(numValue) + strValue print(result)
Output:
19861986
Summary
In conclusion, the “TypeError: unsupported operand type(s) for +: int and str” error arises when we try to use the additional operator between a variable of type ‘int’ and a variable of type ‘string’. The solution to this problem is to convert the string to a number, as shown in the examples above. That’s the end of this post. We hope the information provided will be helpful to you.

My name’s Christopher Gonzalez. I graduated from HUST two years ago, and my major is IT. So I’m here to assist you in learning programming languages. If you have any questions about Python, JavaScript, TypeScript, Node.js, React.js, let’s contact me. I will back you up.
Name of the university: HUST
Major: IT
Programming Languages: Python, JavaScript, TypeScript, Node.js, React.js