-2

I am very new to python world,Please help me how to concatenate str and int objects.

a = 100
b= 200
c = 'Result ='
print (c + a + b)

Throwing error

>>> print (c + a + b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects

I looking output as, Result = 300

Thank in advance...!!!

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
Mohamed Ashraf
  • 91
  • 1
  • 1
  • 4

3 Answers3

3
a = 100
b = 200
c = 'Result = '
print (c + str(int(a + b)))
Januka samaranyake
  • 2,385
  • 1
  • 28
  • 50
1
a = 100
b = 200
c = 'Result = '
print("{}{}".format(c,a+b))

This format of coding is safe as you can change the string variable any time and you don't need to worry about output.

Raj Damani
  • 782
  • 1
  • 6
  • 19
0
a = 100
b = 200
c = 'Result = '
print (c, (a + b))

Even simpler!!!

Actually, you might not need c.

a = 100
b = 200
print ('Result = ', (a + b))
User_Targaryen
  • 4,125
  • 4
  • 30
  • 51