My question is very simple, I want to know that when we initialize a variable in python it recognize it whether it is string or integer, But when we use input function it takes input as string and if we need integer or float we have to type cast it. why?
-
`input` always returns a string. It doesn't attempt to interpret anything. There's a difference between placing a `123` int literal in code, and giving `123` as input to `input`. – Carcigenicate Jun 23 '21 at 14:33
-
1Python 2 did this (`input() == eval(raw_input())`); it's a huge security hole to allow an unknown user to specify an arbitrary Python expression to execute. – chepner Jun 23 '21 at 14:36
-
Suppose you wanted the user to type a password, and they entered 1234, and `input` interpreted that as an integer. Inconvenient. Excel does that when reading phone numbers, dates, and strings like ABCD (which looks like hexadecimal) from csv files and it is very often not what you want. – BoarGules Jun 23 '21 at 14:39
-
@Carcigenicate i know input returns a string but my question is why, If the python is capable of understanding what is the data type of a variable then why he does not know whether the input is integer or string. – waleed sultan Jun 23 '21 at 14:44
-
@waleedsultan As chepner mentioned, you often do not want Python auto-interpretering what the user has entered. What if that user input contains code that Python can interpret? Do you want Python auto-iterpreting everything that the user entered? What if the user entered `print(5)`, and you want that to be taken literally instead of interpreted? – Carcigenicate Jun 23 '21 at 14:46
-
@waleedsultan Because python does not know whether you meant `123` or `"123"` so it keeps everything as a string for consistency and you can rely on the fact that you'll get a string. – Yevhen Kuzmovych Jun 23 '21 at 14:47
-
Related: [What was the reason for removing raw_input() in python 3.x?](https://stackoverflow.com/q/33998175/4518341) – wjandrea Jun 23 '21 at 15:03
-
Could you please [edit] the title to be a clear question? I'm thinking "Why does input() only return strings?" The question doesn't actually have anything to do with variables but *objects*; see [Facts and myths about Python names and values](https://nedbatchelder.com/text/names.html). BTW, welcome to Stack Overflow! Check out the [tour], and [ask] if you want tips. – wjandrea Jun 23 '21 at 15:07
-
1You're talking about *evaluation*. So do these answer your question? [Security of Python's eval() on untrusted strings?](https://stackoverflow.com/questions/661084/security-of-pythons-eval-on-untrusted-strings) and [Why is using 'eval' a bad practice?](https://stackoverflow.com/q/1832940/4518341) – wjandrea Jun 23 '21 at 15:22
2 Answers
Because input()
always returns a str
. In another words, input()
"recognizes" everything (what is entered to it) as a string.
There is a difference between "123"
, which is string and 123
, which is int. To make int from string, you should use type casting - int(input('Number: ')
.
input()
function is not designed to autodetect type, like Python does, because it is inconvenient default behavior. In all usual cases, a program except certain input type from user. You can implement such thing by yourself (if needed).

- 1,291
- 17
- 29
Python CAN recognize type of variable. But most of the time python doesn't NEED to.
As mentioned, input
always returns str
. You need to cast it into int
only if you're gonna do something integer-specific with it. In most cases python doesn't care about type of variables. It is called duck typing
https://realpython.com/lessons/duck-typing/

- 28,235
- 9
- 60
- 81

- 517
- 6
- 18
-
-
It is abouts types and casting. In python. When you understand typing in python given behaviour becomes obvious. – Michail Highkhan Jun 23 '21 at 15:24
-
Duck typing explains "In most cases python doesn't care about type of variables.", but that and duck typing don't seem at all relevant to the question. I think the last two sentences just confuse things. – Carcigenicate Jun 23 '21 at 20:19