Standard Input (stdin) is the default way for a Python program to receive input while it is running. In most cases, input is entered through the keyboard and can be read using the input() function.
name = input("Enter your name: ")
print(f"Hello, {name}!")
Output
Enter your name: Emma
Hello, Emma!
Explanation:
- input() reads a line of text entered by the user.
- The entered value is stored in the variable name.
- print() statement displays a personalized greeting using the user input.
Read Input Using input()
input() function reads data entered through standard input (stdin). It displays a prompt, waits for the user to enter a value and returns the entered data as a string.
message = input("Enter a message: ")
print(message)
Output
Enter a message: Learning Python
Learning Python
Explanation: input("Enter a message: ") displays a prompt and waits for user input. The entered value is stored in the variable message.
Note: input() always returns a string. To work with numeric values, use type conversion functions such as int() or float().
Read Input Using sys.stdin
sys.stdin is a file-like object that reads data directly from the standard input stream. It is useful when processing multiple lines of input or handling large amounts of data efficiently.
import sys
for line in sys.stdin:
if line.rstrip() == "q":
break
print(f"Input: {line.rstrip()}")
print("Exit")
Input
Hello
Python
q
Output
Input: Hello
Input: Python
Exit
Explanation:
- import sys imports the sys module.
- sys.stdin reads input from the standard input stream one line at a time.
- for line in sys.stdin iterates over each line entered by the user.
- rstrip() removes the trailing newline character (\n).
- if line.rstrip() == "q" stops the program when the user enters q.
sys.stdin Operations
1. sys.stdin.readline(): readline() reads one line from stdin and returns it as a string.
import sys
line = sys.stdin.readline()
print("Output:", line.strip())
Input
GeeksforGeeks
Output
Output: GeeksforGeeks
2. sys.stdin.readlines(): readlines() reads all input lines and returns them as a list.
import sys
lines = sys.stdin.readlines()
print(lines)
Input:
Python
Pandas
NumPy
Output
['Python\n', 'Pandas\n', 'NumPy\n']
Explanation: readlines() reads all available input, each line becomes an element of a list. Newline characters (\n) are preserved.
3. sys.stdin.read(): read() reads the entire contents of the input stream at once and returns a single string.
import sys
data = sys.stdin.read()
print(data)
Input:
Hello
Python
GeeksforGeeks
Output
Hello
Python
GeeksforGeeks
Read Input Using fileinput
The fileinput module allows a program to process input from standard input or redirected files using a single interface. It reads the input line by line, making it useful for handling text data from external sources.
import fileinput
for line in fileinput.input():
print(line.rstrip())
Run the program with redirected input:
python program.py < sample.txt
If sample.txt contains:
Hello
Python
Output
Hello
Python
Explanation:
- fileinput.input() creates an input stream from redirected input or files.
- for line in fileinput.input() reads one line at a time.
- rstrip() removes the newline character from the end of each line.
- < sample.txt redirects the contents of the file to the program through standard input.
Note: fileinput is designed for processing redirected input and files. For interactive keyboard input, input() or sys.stdin are generally more suitable.
Comparison of stdin Input Methods
Python provides multiple ways to read data from standard input. The appropriate method depends on how the input is received and how much data needs to be processed. The table below summarizes the purpose of each approach and when it is typically used.
| Method | Use Case |
|---|---|
| input() | Reading interactive user input |
| sys.stdin | Reading multiple lines efficiently |
| sys.stdin.readline() | Reading one line at a time |
| sys.stdin.readlines() | Reading all lines as a list |
| sys.stdin.read() | Reading the entire input stream |
| fileinput.input() | Reading redirected input or multiple files |