+ 1
Why I get 2+3 output as 23 in python?
4 ответов
+ 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)
+ 1
2 and 3 must be string
Use type(2) or type(3) to check whether they are string or not
0
Ok
0
My question is that when i add 2+3 i didn't get output as 5
Instead i get 23