stdin and stdout are file-like objects provided by the OS. In general, when a program is run in an interactive session, stdin is keyboard input and stdout is the user's tty, but the shell can be used to redirect them from normal files or piped output from and input to other programs.

input() is used to prompt the user for typed input. In the case of something like a programming puzzle, it's normally assumed that stdin is redirected from a data file, and when the input format is given it's usually best to use sys.stdin.read() rather than prompting for input with input(). input() is intended for interactive user input, it can display a prompt (on sys.stdout) and use the GNU readline library (if present) to allow line editing, etc.

print() is, indeed, the most common way of writing to stdout. There's no need to do anything special to specify the output stream. print() writes to sys.stdout if no alternate file is given to it as a file= parameter.

Answer from Wooble on Stack Overflow
🌐
University of Washington
faculty.washington.edu › jht › GS559_2017 › lectures › 9B_StdIO_Python.pdf pdf
stdin, stdout, stderr
Though you can treat sys.stdin as if it were an open file, in fact it is sitting in ... The first version has to write the alignment to a file, then read it again. The · second version never reads or writes the alignment. That “|” symbol is read · "pipe" and connects the two programs via stdout ...
Top answer
1 of 5
19

stdin and stdout are file-like objects provided by the OS. In general, when a program is run in an interactive session, stdin is keyboard input and stdout is the user's tty, but the shell can be used to redirect them from normal files or piped output from and input to other programs.

input() is used to prompt the user for typed input. In the case of something like a programming puzzle, it's normally assumed that stdin is redirected from a data file, and when the input format is given it's usually best to use sys.stdin.read() rather than prompting for input with input(). input() is intended for interactive user input, it can display a prompt (on sys.stdout) and use the GNU readline library (if present) to allow line editing, etc.

print() is, indeed, the most common way of writing to stdout. There's no need to do anything special to specify the output stream. print() writes to sys.stdout if no alternate file is given to it as a file= parameter.

2 of 5
5

When you run your Python program, sys.stdin is the file object connected to standard input (STDIN), sys.stdout is the file object for standard output (STDOUT), and sys.stderr is the file object for standard error (STDERR).

Anywhere in the documentation you see references to standard input, standard output, or standard error, it is referring to these file handles. You can access them directly (sys.stdout.write(...), sys.stdin.read() etc.) or use convenience functions that use these streams, like input() and print().

For the Spotify puzzle, the easiest way to read the input would be something like this:

import sys
data = sys.stdin.read()

After these two lines the input for your program is now in the str data.

Discussions

Confused about stdin and stdout
stdin, stdout and stderr have nothing to do with python specifically. These are what we call the main pathways for data to flow in and out of any CLI program. As a beginner, you don't need to worry about them at all, because python wraps them in easy to use input() (for stdin) and print() (for stdout) functions. But for advanced users and very rare usecases we can use the sys modules to access them. https://en.wikipedia.org/wiki/Standard_streams More on reddit.com
🌐 r/learnpython
6
1
September 16, 2022
Standard idiom for reading from stdin, writing to stdout?
I have a bunch of small Python scripts meant to read from stdin and write to stdout if input and/or output files aren’t given, you know, the usual Unix pipeline idiom. Note that I’m not piping within the program, just reading from stdin, writing to stdout (so packages like the pipe package ... More on discuss.python.org
🌐 discuss.python.org
12
0
January 12, 2025
🌐
Python Guides
pythonguides.com › python-stderr-stdin-and-stdout
Python Stdin, Stdout, And Stderr
September 3, 2025 - Learn Python stdin, stdout, and stderr with clear examples. Understand how to handle input, output, and error streams in Python and use them effectively.
🌐
AskPython
askpython.com › home › python – stdin, stdout, and stderr
Python - stdin, stdout, and stderr - AskPython
February 16, 2023 - Python’s sys module provides us with all three file objects for stdin, stdout, and stderr. For the input file object, we use sys.stdin.
🌐
O'Reilly
oreilly.com › library › view › python-in-a › 0596001886 › re118.html
stdin, stdout, stderr - Python in a Nutshell [Book]
March 3, 2003 - Namestdin, stdout, stderr Synopsis stdin , stdout, and stderr are predefined file objects that correspond to Python’s standard input, output, and error streams. You can rebind... - Selection from Python in a Nutshell [Book]
Author   Alex Martelli
Published   2003
Pages   656
🌐
Wellsr
wellsr.com › python › python-stdin-stdout-stderr-with-sys-module
Using Python stdin, stdout, and stderr with the sys Module - wellsr.com
August 9, 2019 - This tutorial will explain how to use shell standard input (stdin), standard output (stdout), and standard error (stderr) in Python programs using the sys module.
🌐
Medium
medium.com › @hhtg250 › stdin-stdout-flush-and-buffering-in-python-e747b85cb6ae
Understanding stdin, stdout, flush(), and Buffering in Python | by Ahmed Nabil - أحمد نبيل | Medium
July 25, 2024 - In Python, handling input and output efficiently is crucial for various applications, from simple scripts to complex data processing. This article delves into the concepts of stdin, stdout, flush(), and buffering, explaining how they work and when to use them.
Find elsewhere
🌐
Quora
quora.com › What-is-the-function-of-STDIN-and-STDOUT-in-Python
What is the function of STDIN and STDOUT in Python? - Quora
Answer (1 of 3): First off, the three standard input streams - standard input (STDIN), standard output (STDOUT) and standard error (STDERR) - are not original or unique to the Python programming language.
🌐
DevDungeon
devdungeon.com › content › using-stdin-stdout-and-stderr-python
Using stdin, stdout, and stderr in Python | DevDungeon
February 15, 2020 - To pipe the standard output of one program to the standard input of your Python program, you can do it like this: ... The dunder properties sys.__stdin__, sys.__stdout__ and sys.__stderr__ always contain references to the original streams.
🌐
Python Course
python-course.eu › material › applied_python.pdf pdf
bodenseo Applied Python Bernd Klein
March 5, 2020 - minor, micro, release-level, and serial. The values of this tuple are · integers except the value for the release level, which is one of the · following: 'alpha', 'beta', 'candidate', or 'final'. >>> sys.version_info · (2, 6, 5, 'final', 0) >>> __stdin__ __stdout__ __stderr__ These attributes contain the original values of stdin, stderr and ·
🌐
Plain English
python.plainenglish.io › stdin-stdout-and-stderr-for-mortals-3b2d0f3b8f42
stdin, stdout, and stderr For Mortals | by Rahul Beniwal | Python in Plain English
July 25, 2024 - stdin, stdout, and stderr For Mortals Don’t confuse 0, 1, and 2 anymore. Hello Everyone, Today I am going to discuss the fundamental concept of OS. These concepts are a must for every developer as …
🌐
NetworkLessons
notes.networklessons.com › home › python › python print function
Python stdin, stdout, stderr - Notes
February 9, 2026 - This tells Python to execute the function. We don’t see anything on our screen, but this produces an invisible newline character, which shows up as a blank line on your screen.
🌐
Diveintopython
book.diveintopython.org › scripts_and_streams › stdin_stdout_stderr.html
10.2. Standard input, output, and error
[you@localhost kgp]$ python kgp.py -g binary.xml 01100111 [you@localhost kgp]$ cat binary.xml <?xml version="1.0"?> <!DOCTYPE grammar PUBLIC "-//book.diveintopython.org//DTD Kant Generator Pro v1.0//EN" "kgp.dtd"> <grammar> <ref id="bit"> <p>0</p> <p>1</p> </ref> <ref id="byte"> <p><xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/>\ <xref id="bit"/><xref id="bit"/><xref id="bit"/><xref id="bit"/></p> </ref> </grammar> [you@localhost kgp]$ cat binary.xml | python kgp.py -g - 10110001 · So how does the script “know” to read from standard input when the grammar file is “-”? It's not magic; it's just code. def openAnything(source): if source == "-": import sys return sys.stdin # try to open with urllib (if source is http, ftp, or file URL) import urllib try: [... snip ...]
🌐
University Information Services
help.uis.cam.ac.uk › system › files › documents › subprocess.pdf pdf
Python 3: Child processes Bob Dowling rjd4@cam.ac.uk 29 October 2012
beta_p = subprocess.Popen(['wc'], stdin=alpha_p.stdout) ... More than one command joined together like this is called a “pipeline” and pipelines can have as many ... We have seen how to launch two (or more) programs and have them communicate. What we will look at next · is launching one program and reading its output from the Python script itself.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-print-to-stderr-and-stdout-in-python
How to print to stderr and stdout in Python? - GeeksforGeeks
July 12, 2025 - In Python, whenever we use print() the text is written to Python’s sys.stdout, whenever input() is used, it comes from sys.stdin, and whenever exceptions occur it is written to sys.stderr.
🌐
Datacamp
projector-video-pdf-converter.datacamp.com › 16871 › chapter2.pdf pdf
Execute shell commands in subprocess COMMAND LINE AUTOMATION IN PYTHON
COMMAND LINE AUTOMATION IN PYTHON · Using Unix Pipes as input · Two ways of connecting input · Popen method · proc1 = Popen(["process_one.sh"], stdout=subprocess.PIPE) Popen(["process_two.sh"], stdin=proc1.stdout) run method (Higher Level Abstraction) proc1 = run(["process_one.sh"], stdout=subprocess.PIPE) run(["process_two.sh"], input=proc1.stdout) COMMAND LINE AUTOMATION IN PYTHON ·
🌐
Mykvs
python.mykvs.in › uploads › tutorials › XIIComp.Sc.25.pdf pdf
Opening and Closing Files
Python File Streams · Standard Input, Output and Error Streams · • · There are three different standard streams · Standard input device (stdin) – reads from the keyboard · Standard output device (stdout) – prints to the display and can be · redirected as standard input.