HOWTO · Python
How to Read Input From stdin in Python
Learn when to use input(), sys.stdin, and fileinput.input() to read a line, a stream, or filenames with standard-input fallback in Python.
On this page
Standard input, usually called stdin, is a text stream a Python process can read. A terminal can provide it interactively, or another command can supply it through a pipe or redirection. These are different ways to provide data to the same stream; they do not require opening a named file in the program. The right API depends on whether the program expects one response, many lines, or either filenames or a stream.
How standard input works in Python
When a program reads from standard input, it consumes data until it has enough or reaches end-of-file (EOF). In a terminal, EOF is sent with the platform’s end-of-input key sequence; in a pipe or redirected file, it occurs when the producer closes the stream or the file ends. A process waiting for more input has not necessarily stalled: it may be waiting for another line or EOF.
input() reads one line and returns its contents as a string, without the line-ending character. sys.stdin exposes the stream itself, so code can iterate over lines or call methods such as read() to consume the rest. fileinput.input() adds filename handling: it processes filenames passed on the command line and uses standard input if no filenames are given.
Read one line with input()
Use input() when the program asks for one response, such as a name or a value. It can display a prompt, waits for one line, and returns a string. Convert that string explicitly when the next operation needs a number; for example, int() converts whole-number input. A prompt is useful for an interactive terminal, while scripts that receive piped input often omit it so it does not mix with machine-readable output.
Run this example:
import io
import sys
sys.stdin = io.StringIO("Ada\n3\n")
name = input()
count = int(input())
print(f"{name} has {count} items")
The in-memory stream makes this example self-contained; a real script normally reads the process’s existing sys.stdin instead. In a terminal, enter a name and then a whole number, pressing Enter after each line. The program converts only the second response; input() itself returns text. If input comes from a pipe or redirection, the supplied stream must contain both lines.
Read lines with sys.stdin
For input with an unknown number of lines, iterate over sys.stdin. Each yielded line normally retains its line ending. Calling rstrip("\n") removes newline characters at the end while preserving other whitespace, unlike strip(), which also removes spaces and tabs that may be meaningful data.
import sys
for line in sys.stdin:
value = line.rstrip("\n")
print(value.upper())
The loop continues until EOF, so in a terminal send the end-of-input key sequence when finished. In a pipe, the upstream command ending closes the stream. To consume all remaining input as a single string instead, use sys.stdin.read(); this waits for EOF and can use substantial memory for a large stream, so line iteration is usually a better fit for large input.
Read files or standard input with fileinput.input()
Use fileinput.input() when the same line-processing script should accept one or more filenames as command-line arguments and also work as a filter. With filenames, it reads those files in order; with no filenames, it reads standard input. This lets a caller choose between a named input file and a pipe without changing the processing loop.
import fileinput
for line in fileinput.input():
print(line.rstrip("\n").upper())
For example, the script can be invoked with a filename, or another command can pipe text into it with no filename argument. The displayed code is unexecuted in the supplied packet; no sample output is asserted here. See the Python fileinput documentation for options such as reporting the current filename and line number.
Choose the right approach
Choose input() for a prompt followed by one line, especially when a person is interacting with the program. Choose sys.stdin when the program processes an arbitrary stream, needs to handle many lines, or should consume the complete stream with read(). Choose fileinput.input() when the same line-oriented program should process command-line filenames or fall back to standard input.
Remember that input() returns text, even when the response looks numeric. For stream processing, decide whether each line’s surrounding whitespace is data before stripping it. For whole-stream reads, account for the fact that the program finishes only after EOF. These distinctions make it easier to select an API that matches the source and size of the input.