. Advertisement .
..3..
. Advertisement .
..4..
I don’t know what I’m doing wrong, but I’ve already lost a couple of days struggling with this. Here is my command line:
a = raw_input("Enter a: ")
b = raw_input("Enter b: ")
print "a + b as strings: " + a + b
a = int(a)
b = int(b)
c = a + b
str(c)
print "a + b as integers: " + c
This returns:
Python: TypeError: cannot concatenate 'str' and 'int' objects
I don’t have any experience with the “typeerror: cannot concatenate ‘str’ and ‘int’ objects.” In this case, how should I change?
The cause: The issue can be produced by the last
print
statement.The solution: There are two options for resolving the issue. The result of the
str(c)
function can be assigned toc
and then concatenate all of the strings or simply use the following to replace the lastprint
:in which case
is not required and can be removed.
The following is the output of the sample run:
with:
str(c)
returns an new string representation forc
and does not modifyc
.This is most likely what you are searching for