The error TypeError: cannot concatenate ‘str’ and ‘int’ objects is due to concatenation of an integer value with a string object. You can not concatenate an integer value with a string directly in the python.
Python does not support the auto type of the variable to be cast. You can not concatenate an integer value to a string. The data type of the two variables should be the string to be concatenated. Python allows you to convert an integer to a string using built-in functions such as str(), format() etc.
The mismatch between the data type of the two variables causes an error “TypeError: cannot concatenate ‘str’ and ‘int’ objects” while concatenating. Before concatenating, both variables should be converted to a string object.
Different variations of the error
There are different variations of this type of error due to different data types. The solutions to these errors are the same.
TypeError: cannot concatenate 'str' and 'int' objects
TypeError: Can't convert 'int' object to str implicitly
TypeError: can only concatenate str (not "float") to str
TypeError: cannot concatenate 'str' and 'float' objects
TypeError: cannot concatenate 'str' and 'bool' objects
TypeError: cannot concatenate 'str' and 'NoneType' objects
TypeError: cannot concatenate 'str' and 'list' objects
Exception
The error “TypeError: cannot concatenate ‘str’ and ‘int’ objects” will be shown as below the stack trace.
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 2, in <module>
print ("the value is " + x)
TypeError: cannot concatenate 'str' and 'int' objects
[Finished in 0.1s with exit code 1]
Root Cause
Python does not support the auto type of the variable to be cast. You can not concatenate an integer value to a string. The root cause of this issue is due to the concatenation between an integer value and a string object. Before concatenation, an integer value should be converted to a string object.
Solution 1
Python has a built-in function str() to convert an integer value to a string object. The integer value should be converted using str() function before the integer value is concatenated with a string. The str() functions takes an integer value as argument and returns a string.
Program
x= 5
print ("the value is " + x)
Output
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 2, in <module>
print ("the value is " + x)
TypeError: cannot concatenate 'str' and 'int' objects
[Finished in 0.1s with exit code 1]
Solution
x= 5
print ("the value is " + str(x))
Output
the value is 5
[Finished in 0.1s]
Solution 2
The python print statement has the option of passing two values with different data types. The print statement converts internally to the string value and prints the value.
Program
x= 5
print ("the value is " + x)
Output
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 2, in <module>
print ("the value is " + x)
TypeError: cannot concatenate 'str' and 'int' objects
[Finished in 0.1s with exit code 1]
Solution
x= 5
print ("the value is " , x)
Output
the value is 5
[Finished in 0.1s]
Solution 3
The python built-in function format() allow you to format a string with dynamic value insertion. The format() function takes arguments with different datatypes and dynamically inserts the values in the respective format specifier place in the string.
Program
x= 5
print ("the value is " + x)
Output
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 2, in <module>
print ("the value is " + x)
TypeError: cannot concatenate 'str' and 'int' objects
[Finished in 0.1s with exit code 1]
Solution
x= 5
print ("the value is {0}".format(x))
Output
the value is 5
[Finished in 0.1s]