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…!!!
Answers:
a = 100
b = 200
c = 'Result = '
print (c + str(int(a + b)))
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))
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.
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…!!!
a = 100
b = 200
c = 'Result = '
print (c + str(int(a + b)))
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))
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.