r/learnpython • • Nov 03 '24

how is 3+5 38? [input function]

SOLVED

this is the code:

a=input("value of a: ")
b=input("value of b: ")
print(3+5)
print(a+b)
print(type(a))

its output:

value of a: 3
value of b: 5
8
35
<class 'str'>

I know the default datatype for input function is string, so i thought it would give an error. But the output is somehow 35? Can someone explain whats going on?

another eg:-

input:

a=input("value of a: ")
b=input("value of b: ")
print(4+6)
print(a+b)
print(type(a))

output:

value of a: 4
value of b: 6
10
46
<class 'str'>
0 Upvotes

11 comments sorted by

View all comments

8

u/eleqtriq Nov 03 '24

The input() function in Python returns a string, so when you use a=input("value of a: ") and b=input("value of b: "), both a and b are strings. When you use print(a+b), you're concatenating these two strings, not adding them as numbers. That's why you're seeing 35 and 46 instead of 8 and 10.

-4

u/Winter-Drama2001 Nov 03 '24

ohhhhhhh understood 🙇 thank you