How can I add two variable and assign to result variable in Python?

Question:

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…!!!

Asked By: Mohamed Ashraf

||

Answers:

a = 100
b = 200
c = 'Result = '
print (c + str(int(a + b)))
Answered By: Januka samaranyake
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))
Answered By: User_Targaryen
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.

Answered By: Raj Damani
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.