Why using string? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why using string?

elif user_input == "add": num1 = float(input("Enter a number: ")) num2 = float(input("Enter another number: ")) result = str(num1 + num2) print("The answer is " + result) Why in this program are we converting the sum to string value?

19th Jun 2019, 10:22 AM
DEVANSH BANSAL
DEVANSH BANSAL - avatar
2 Answers
+ 3
python can't add a int/float to a string so it needs to be converted to string. if you just do 'print(result)' you don't need to convert it to string. By the way in other languages like Java/Javascript/... you add them and the conversion happens automatically, but not in python!
19th Jun 2019, 10:27 AM
Anton Böhler
Anton Böhler - avatar
+ 1
Thanks
19th Jun 2019, 10:28 AM
DEVANSH BANSAL
DEVANSH BANSAL - avatar