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.


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


Enter second number

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 *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock