Reputation: 1
If I have such a simple code
varry = 1
print(type(varry))
it gives me - class 'int'
But if I have this:
varry = input()
print(type(varry))
And I type 1, it gives me class 'str'
Tell me please - why is it so and how should I write a program so that it defines a variable I enter correctly as int, str or float?
Upvotes: -1
Views: 2486
Reputation: 325
The built-in function input
always returns a string, regardless of whether that string might be comprised of solely numerical characters. If you are sure the result can represent a number, then convert it:
varry = int(input())
Now the type of varry
will be int
, assuming this conversion doesn't raise a ValueError
Upvotes: 3
Reputation: 44838
What you input to your program is always of type str
. if you want Python to deduce the type of the input itself, use data = eval(input())
or, for more safety:
import ast
data = ast.literal_eval(input())
Upvotes: 3
Reputation: 827
varry = int(input())
print(type(varry))
You have to convert it to int using int() method
Upvotes: 0