+ 1

Why I get 2+3 output as 23 in python?

15th May 2020, 1:18 PM
Nishant Bhatte
Nishant Bhatte - avatar
4 odpowiedzi
+ 3
if you use an input to get the two numbers from the keyboard, like this: a = input() -> you type 2 b = input() -> you type 3 print(a+b) -> you will get concatenation because the inputs default to strings: '2' + '3' = '23' instead you can do this: a = int(input()) -> you type 2 b = int(input()) -> you type 3 print (a+b) -> since a and b are now int, they will be evaluated as math addition meaning: 2 + 3 = 5 (notice no more quotes, because they are no longer strings)
15th May 2020, 2:05 PM
Sebastian Pacurar
Sebastian Pacurar - avatar
+ 1
2 and 3 must be string Use type(2) or type(3) to check whether they are string or not
15th May 2020, 1:19 PM
Abhay
Abhay - avatar
0
Ok
15th May 2020, 1:20 PM
Nishant Bhatte
Nishant Bhatte - avatar
0
My question is that when i add 2+3 i didn't get output as 5 Instead i get 23
15th May 2020, 1:27 PM
Nishant Bhatte
Nishant Bhatte - avatar