+ 2

In Python3, using variables, i've done addition so that i got a=5. Now, in next line i want to get output as: The sum is 5. How?

Means... should I do print ("the sum is" + a) ? This I tried but I got error that int and string can't be contained in same line. So...how should I program to get "The sum is 5"?

21st Jun 2019, 7:47 AM
Chaitanya's Stuff
Chaitanya's Stuff - avatar
2 Answers
+ 5
Chaitanya's Stuff The problem is that you‘re trying to add an integer to a string, which is not allowed. Instead, you can try the following: ———————————- print(‘The sum is’, a) or print(‘The sum is’+str(a)) ——————————— Hope this helps :)
21st Jun 2019, 7:58 AM
aceisace
aceisace - avatar
+ 3
That error is because you tried to add string to an integer, in Java and JavaScript that is possible, but in Python it raises an error. ace already gave a few working methods, but you might also be interested in the str.format method. print("The sum is {}".format(a)) print("The sum is {sum}".format(sum=a)) print("The {1} is {0}".format(a, "sum")) #They would all print: The sum is 5
21st Jun 2019, 8:44 AM
Seb TheS
Seb TheS - avatar