Python TypeError cannot concatenate ‘str’ and ‘int’ objects

When I am trying to write a small python script I got following error:

TypeError: cannot concatenate ‘str’ and ‘int’ objects

Example code where we can reproduce similar error is below:

a=1
b=2
c=a+b
print 'sum is ' + c

After debugging above error the root cause for it is ‘sum is’ a string an c is an integer, since both are different types print statement is unable to concatenate them.

So I just changed the last line of above as below and the script is worked without any issue.

print 'sum is ' + str(c)

-Sany

Advertisements
By Sandeep Posted in python

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s