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
🌐
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.
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

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
Stdin Stdout python - Stack Overflow
For my work i'm used to work with matlab. No i try to learn the basic skills of python as well. Currently I'm working on on the following excersise: You are interested in extracting all of the More on stackoverflow.com
🌐 stackoverflow.com
python - Stdin and Stdout - Stack Overflow
Can someone explain stdin and stdout? I don't understand what is the difference between using these two objects for user input and output as opposed to print and raw_input. Perhaps there is some vi... More on stackoverflow.com
🌐 stackoverflow.com
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
🌐
GeeksforGeeks
geeksforgeeks.org › python › take-input-from-stdin-in-python
Take input from stdin in Python - GeeksforGeeks
July 12, 2025 - There are a number of ways in which we can take input from stdin in Python. ... First we need to import sys module. sys.stdin can be used to get input from the command line directly. It used is for standard input. It internally calls the input() method. Furthermore, it, also, automatically adds '\n' after each sentence. ... import sys for line in sys.stdin: if 'q' == line.rstrip(): break print(f'Input : {line}') print("Exit") ... The input() can be used to take input from the user while executing the program and also in the middle of the execution.
🌐
AskPython
sallyscorners.com › home › python – stdin, stdout, and stderr
Python - stdin, stdout, and stderr - AskPython / sys.stdout.write in Python - GeeksforGeeks
February 16, 2023 - Standard error – The user program writes error information to this file-handle. Errors are returned override the Standard error (stderr). Python provides states with file-like objects that represent stdin, stdout, and stderr.
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-print-to-stderr-and-stdout-in-python › amp
How to print to stderr and stdout in Python? - GeeksforGeeks
April 28, 2025 - stdout in Python is similar to sys.stdin, but it directly displays anything written to it to the Console.
🌐
Python Guides
pythonguides.com › python-stderr-stdin-and-stdout
Python – stdin stdout and stderr [6 Examples] - Python Guides
September 3, 2025 - Think of them as three communication channels between your Python program and the outside world. The simplest way to use stdin and stdout is through the input() and print() methods in Python.
Find elsewhere
🌐
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.
🌐
AskPython
askpython.com › home › python – stdin, stdout, and stderr
Python - stdin, stdout, and stderr - AskPython
February 16, 2023 - We give input to the standard input (stdin). Standard output – The user program writes normal information to this file-handle. The output is returned via the Standard output (stdout).
🌐
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.
🌐
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.
🌐
Wikipedia
en.wikipedia.org › wiki › Standard_streams
Standard streams - Wikipedia
3 weeks ago - #!/usr/bin/env python import sys from typing import TextIO if __name__ == "__main__": # Save the current stdout so that we can revert sys.stdout # after we complete our redirection stdin_fileno: TextIO = sys.stdin stdout_fileno: TextIO = sys.stdout # Redirect sys.stdout to the file sys.stdout: TextIO = open("myfile.txt", "w") ctr: int = 0 for inps in stdin_fileno: ctrs: str = str(ctr) # Prints to the redirected stdout () sys.stdout.write(f"{ctrs}) this is to the redirected --->{inps}\n") # Prints to the actual saved stdout handler stdout_fileno.write(f"{ctrs}) this is to the actual --->{inps}\n") ctr = ctr + 1 # Close the file sys.stdout.close() # Restore sys.stdout to our old saved file handler sys.stdout = stdout_fileno
🌐
NetworkLessons
notes.networklessons.com › home › python › python print function
Python stdin, stdout, stderr - Notes
February 27, 2020 - 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.
🌐
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 - Error messages and diagnostic output are usually sent to stderr to keep them separate from regular output (stdout). Python provides ways to interact with stdin, stdout, and stderr through the sys module in its standard library.