How to read input in Python?
In Python 3, input() function is used for taking input from the keyboard. The input() function in Python 3 is different from Python 2.
In Python 3, input treats everything as a string, while in Python 2 it is not.
For example, if you try to read integer value 10 using input() in Python 3, it will consider 10 as string.
But in Python 2, it remains as it is.
Lets have a look at the code given below,
Program
#Program: To take the input from keyboard
#Author: Hitendra E. Suryavanshi
a=input("Enter any value:")
print("Value is ",a)
print("Data type of a is:",type(a))
Output
Here, you can see data type of 'a' is 'str' and not 'int'. Because, in Python 3, everything is returned as string. If you try with other values, like float, it is also considered as string.
How can we solve this problem?
Typecasting:
We have to use typecasting (explicit) in order to solve the above problem.
#Program: To take the input from keyboard
#Author: Hitendra E. Suryavanshi
a=int(input("Enter any value:"))
print("Value is ",a)
print("Data type of a is:",type(a))
Output:
Here, the value of a is 10 and so the data type is 'int'.
How can we read single character in Python?
In Python, unlike C, we have only one data type i.e. string (str), not Char.
Therefore you can read single character or multiple character at the same time. But all of them ar treated as a string.
Ex. a="c"
Ex. b="cat"
Here, the data type of both a and b are string.
Thank you
H. E. Suryavanshi
In Python 3, input() function is used for taking input from the keyboard. The input() function in Python 3 is different from Python 2.
In Python 3, input treats everything as a string, while in Python 2 it is not.
For example, if you try to read integer value 10 using input() in Python 3, it will consider 10 as string.
But in Python 2, it remains as it is.
Lets have a look at the code given below,
Program
#Program: To take the input from keyboard
#Author: Hitendra E. Suryavanshi
a=input("Enter any value:")
print("Value is ",a)
print("Data type of a is:",type(a))
Output
Enter any value:10 Value is 10 Data type of a is: <class 'str'>
Here, you can see data type of 'a' is 'str' and not 'int'. Because, in Python 3, everything is returned as string. If you try with other values, like float, it is also considered as string.
How can we solve this problem?
Typecasting:
We have to use typecasting (explicit) in order to solve the above problem.
#Program: To take the input from keyboard
#Author: Hitendra E. Suryavanshi
a=int(input("Enter any value:"))
print("Value is ",a)
print("Data type of a is:",type(a))
Output:
Enter any value:11 Value is 11 Data type of a is: <class 'int'>
Here, the value of a is 10 and so the data type is 'int'.
How can we read single character in Python?
In Python, unlike C, we have only one data type i.e. string (str), not Char.
Therefore you can read single character or multiple character at the same time. But all of them ar treated as a string.
Ex. a="c"
Ex. b="cat"
Here, the data type of both a and b are string.
Thank you
H. E. Suryavanshi