Nearby lessons
158 of 159Python - Input and Output Statements
- Explain the purpose of input and output statements
- Distinguish Python 2 input functions from the Python 3 input() function
- Read integer, float and boolean input using type casting
- Read multiple values from one line using split()
- Evaluate expressions dynamically using eval()
Introduction
Input and Output statements are used to interact with the user.
They help us:
- Read input from the keyboard.
- Display output on the screen.
Reading Dynamic Input from the Keyboard
Python provides functions to read values entered by the user.
The available functions are different in Python 2 and Python 3.
Input Functions in Python 2
Python 2 provides two input functions:
raw_input()input()
1. raw_input()
The raw_input() function always reads input as a string.
If another data type is required, we must convert it using type casting.
Example - raw_input()
Important Note
For every type of input, raw_input() always returns a string.
2. input() in Python 2
The input() function in Python 2 reads input directly in the required data type.
Type casting is generally not required.
Example - input() in Python 2
Python 3 input() Function
Python 3 provides only the input() function.
The raw_input() function is not available.
Python 3 input() works like Python 2 raw_input().
This means every input value is treated as a string.
Reading Integer Input
Since input() returns a string, use int() to convert it into an integer.
Example - Integer Input
Reading Float Input
Use float() to convert the input into a floating-point number.
Example - Float Input
Reading Boolean Input
Be careful here: bool("False") is True, because any non-empty string is truthy. So casting the raw input with bool() does not work for a True/False choice.
To parse it correctly, compare the string instead.
Example - Boolean Input
Important Note
bool(input(...)) is a common trap: entering False would still print True, because any non-empty string is truthy.
The comparison version handles both cases correctly — entering True prints True, and entering False prints False.
Complete Example
Reading Multiple Values in One Line
The split() method is used to read multiple values entered in a single line.
Example - Multiple Integer Values
Important Note
By default, split() uses a space as the separator.
You can also specify another separator if needed.
Reading Multiple Float Values Using Comma
eval() Function
The eval() function takes a string and evaluates it as a Python expression.
Example 1 - eval()
Example 2 - eval() with User Input
Python 2 vs Python 3 Input Functions
| Python 2 | Python 3 |
|---|---|
Provides raw_input() and input(). |
Provides only input(). |
raw_input() returns a string. |
input() returns a string. |
Real World Usage
Input and Output statements are commonly used in:
- Login systems
- Student management systems
- Banking applications
- Online forms
- Console-based applications
Important Notes
- Python 2 provides both
raw_input()andinput(). - Python 3 provides only
input(). - Python 3
input()always returns a string. - Use type casting such as
int(),float(), andbool()to convert input into the required data type. - Do not use
bool()to parse aTrue/Falsestring —bool("False")isTrue. Compare the string instead. split()is used to read multiple values from one line.eval()evaluates a string as a Python expression.
Quick Summary
| Function | Purpose |
|---|---|
raw_input() |
Reads input as a string (Python 2). |
input() (Python 2) |
Reads input in the required data type. |
input() (Python 3) |
Reads input as a string. |
split() |
Reads multiple values from one line. |
eval() |
Evaluates a string expression. |
- Output statements display results and input statements read values from the user
- Python 2 provided raw_input() and input(); Python 3 provides only input()
- Python 3 input() always returns a string, so use int(), float() or bool() to cast it
- split() reads multiple values entered on a single line
- eval() evaluates a string as a Python expression