157

I have this python program that adds strings to integers:

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

I get this error:

TypeError: cannot concatenate 'str' and 'int' objects

How can I add strings to integers?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
plahstic
  • 1,621
  • 2
  • 11
  • 7

9 Answers9

187

There are two ways to fix the problem which is caused by the last print statement.

You can assign the result of the str(c) call to c as correctly shown by @jamylak and then concatenate all of the strings, or you can replace the last print simply with this:

print "a + b as integers: ", c  # note the comma here

in which case

str(c)

isn't necessary and can be deleted.

Output of sample run:

Enter a: 3
Enter b: 7
a + b as strings:  37
a + b as integers:  10

with:

a = raw_input("Enter a: ")
b = raw_input("Enter b: ")
print "a + b as strings: " + a + b  # + everywhere is ok since all are strings
a = int(a)
b = int(b)
c = a + b
print "a + b as integers: ", c
Levon
  • 138,105
  • 33
  • 200
  • 191
86

str(c) returns a new string representation of c, and does not mutate c itself.

c = str(c) 

is probably what you are looking for

jamylak
  • 128,818
  • 30
  • 231
  • 230
23

If you want to concatenate int or floats to a string you must use this:

i = 123
a = "foobar"
s = a + str(i)
Wooble
  • 87,717
  • 12
  • 108
  • 131
Thargor
  • 1,862
  • 14
  • 24
6
c = a + b 
str(c)

Actually, in this last line you are not changing the type of the variable c. If you do

c_str=str(c)
print "a + b as integers: " + c_str

it should work.

user2919374
  • 61
  • 1
  • 1
3

Apart from other answers, one could also use format()

print("a + b as integers: {}".format(c))

For example -

hours = 13
minutes = 32
print("Time elapsed - {} hours and {} minutes".format(hours, minutes))

will result in output - Time elapsed - 13 hours and 32 minutes

Check out docs for more information.

Ananth
  • 2,597
  • 1
  • 29
  • 39
2

You can convert int into str using string function:

user = "mohan"

line = str(50)

print(user + "typed" + line + "lines")
Hugo
  • 27,885
  • 8
  • 82
  • 98
1

The easiest and least confusing solution:

a = raw_input("Enter a: ")
b = raw_input("Enter b: ")
print "a + b as strings: %s" % a  + b
a = int(a)
b = int(b)
c = a + b
print "a + b as integers: %d" % c

I found this on http://freecodeszone.blogspot.com/

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Shekhar
  • 75
  • 1
  • 1
1

I also had the error message "TypeError: cannot concatenate 'str' and 'int' objects". It turns out that I only just forgot to add str() around a variable when printing it. Here is my code:

def main():
 rolling = True; import random
 while rolling:
  roll = input("ENTER = roll; Q = quit ")
  if roll.lower() != 'q':
   num = (random.randint(1,6))
   print("----------------------"); print("you rolled " + str(num))
  else:
   rolling = False
main()

I know, it was a stupid mistake but for beginners who are very new to python such as myself, it happens.

djmurps
  • 21
  • 1
1

This is what i have done to get rid of this error separating variable with "," helped me.

# Applying BODMAS 
arg3 = int((2 + 3) * 45 / - 2)
arg4 = "Value "
print arg4, "is", arg3

Here is the output

Value is -113

(program exited with code: 0)

Mansur Ul Hasan
  • 2,898
  • 27
  • 24