![]() |
VOOZH | about |
In Python, both input() and raw_input() functions are used to take user input. But:
Let’s break this down clearly with explanations and examples.
It is used to take input from user as a string. It waits for user to type something and press Enter, then returns that input as a string which can be stored or processed in program.
Example:
Output
Enter the name: python3
<class 'str'>
python3Enter the number: 1997
<class 'str'>
<class 'int'>
1997
Explanation:
In Python 2, things are a bit different:
Example:
Output
Enter the name: "python3"
<type 'str'>
python3Enter the number: 1997
<type 'int'>
1997
Explanation:
The raw_input() function in Python 2 was designed to avoid confusion.
Example:
Output
Enter the name: python3
<type 'str'>
python3Enter the number: 1997
<type 'str'>
<class 'int'>
1997
Explanation:
Let's see difference in tabular form:
| Feature | input() (Python 2.x) | raw_input() (Python 2.x) | input() (Python 3.x) |
|---|---|---|---|
| Availability | Python 2 only | Python 2 only | Python 3 only |
| Return Type | Evaluates input (string, int, float, etc. depending on what you type) | Always returns string | Always returns string |
| Need for quotes | Yes, for strings | No quotes needed | No quotes needed |
| Conversion needed | Not always (auto-detects type) | Yes (must convert manually) | Yes (must convert manually) |
| Status | Deprecated in Python 3 | Deprecated in Python 3 | Actively used |