Input from keyboard in python

In python, we use the input() method to take input from the keyboard. The data collected by input() is in string data type by default.

Example:

print("Enter the first number")
a = input()
print()
print("Enter second number")
b = input()
c = a + b
print("Result",c)
print()
print("Type of A", type(a))
print()
print("Type of B",type(b))
print()
print("Type of C",type(c))
Output:
Enter the first number

12

Enter second number

23
Result 1223

Type of A <class 'str'>

Type of B <class 'str'>

Type of C <class 'str'>

Try to understand the example, in the above example you can see, we are taking input from the keyboard using the input() method. After taking input of a and b we do some “a+b” and store the value in another variable “c”. In variable “a” we are storing value 12 and in variable “b” we are storing value 23. so, now you are thinking in variable “c”, the sum of 12 and 23 (12+23 = 35)should be stored but actually, 1223 is storing in variable “c”. Now the question is why so? In python, the data collected by input() is in string data type by default. So instead of doing sum, the above program concatenating string. And also if you check the data type. It is showing string type.

Leave a Comment

Your email address will not be published. Required fields are marked *