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

Forcing sys.stdin/stdout/stderr encoding/newline behavior programmatically
I learned the hard way that Python does not always use UTF-8 for sys.stdin/stderr/stdout. In order to make my own tools always work properly as pipes using UTF-8 encoding and also handle new lines properly (like open would do by default), I would like to set this programmatically for all Python ... More on discuss.python.org
🌐 discuss.python.org
6
0
May 2, 2022
python - How do I read from stdin? - Stack Overflow
When using -c command, as a tricky way, instead of reading the stdin (and more flexible in some cases) you can pass a shell script command as well to your python command by putting the shell command in quotes within a parenthesis started by $ sign. More on stackoverflow.com
🌐 stackoverflow.com
November 6, 2011
What exactly are stdout, stdin, and stderr?
When you start a new process, it starts up with three standard file descriptors (or streams, or whatever they may be called in different languages). These are: stdin, which is the program's standard input stream, from where it is expected to read its input; stdout, which is the program's standard output stream, to where it is expected to write its normal output; and stderr, the program's error stream, to where it is expected to write any error messages. All three are automatically inherited from the parent process unless the parent process does something about it. In particular, when you start a program from the shell with no redirections, all three standard streams are connected to the terminal, just as for the shell. So the program reads input from the keyboard through stdin, and prints output to the terminal screen through stdout, and if it prints any error messages they are also shown in the terminal. If you start a program with output redirection, as in some_prog > out.txt, the shell connects the program's stdout to the specified file. When the program writes to stdout, it goes into that file. But the error messages still go to the terminal, so the user sees them. Very useful. If you start a program with input redirection, as in some_prog < in.txt, the shell connects the program's stdin to the specified file. So when the program reads input from stdin, it will come from the file. Nothing the user types on the keyboard is seen by the program. When you run multiple programs in a pipeline, as in ls | less, the shell creates a pipe, connects the stdout of the first process to the write side of the pipe, and connects the stdin of the second process to the read side of the pipe. So anything the first program prints on stdout is read by the second program on stdin. More on reddit.com
🌐 r/learnprogramming
5
1
May 2, 2022
How to do stdin and stdout in binary?

you can output bytes directly to the terminal by writing to sys.stdout.buffer and read from sys.stdin.buffer

edit: here is an example program:

import sys
for chunk in sys.stdin.buffer.read():
    # bypass the encoding process
    sys.stdout.buffer.write(chunk)

and calling it to make a copy of a file:

protoss@pylon~$ wr_bin.py < in.bin > out.bin
More on reddit.com
🌐 r/learnpython
4
2
October 14, 2019
🌐
AskPython
askpython.com › home › python – stdin, stdout, and stderr
Python - stdin, stdout, and stderr - AskPython
February 16, 2023 - Before going through this article, ... stdout and stderr are. Standard input – This is the file-handle that a user program reads to get information from the user. We give input to the standard input (stdin).
🌐
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.
🌐
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.
🌐
Python documentation
docs.python.org › 3 › tutorial › inputoutput.html
7. Input and Output — Python 3.14.3 documentation
There are several ways to present the output of a program; data can be printed in a human-readable form, or written to a file for future use. This chapter will discuss some of the possibilities. So far we’ve encountered two ways of writing values: expression statements and the print() function. (A third way is using the write() method of file objects; the standard output file can be referenced as sys.stdout.
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.
🌐
O'Reilly
oreilly.com › library › view › python-essential-reference › 0672328623 › 0672328623_ch09lev1sec3.html
Python: Essential Reference, Third Edition
February 20, 2006 - stdout is the file object that receives output produced by print.stderr is a file that receives error messages. More often than not, stdin is mapped to the user’s keyboard, whereas stdout and stderr produce text onscreen.
Author   David Beazley
Published   2006
Pages   648
🌐
CLIN
let.rug.nl › vannoord › College › Zoekmachines › stdin.pdf pdf
Standard input and standard output and Python Gertjan van Noord
Standard input and standard output and Python · Gertjan van Noord · Recall: stdin, stdout, stderr · Unix commands typically take some input, and provide output. If you don’t give any particular details, the input is taken from the · keyboard, and the output is sent to the screen ·
🌐
JournalDev
journaldev.com › 32137 › read-stdin-python
How to Read from stdin in Python | DigitalOcean
August 3, 2022 - Hi Processing Message from sys.stdin *****Hi ***** Hello Processing Message from sys.stdin *****Hello ***** Exit Done ... Notice the use of rstrip() to remove the trailing newline character so that we can check if the user has entered “Exit” message or not. We can also use Python input() function to read the standard input data. We can also prompt a message to the user. Here is a simple example to read and process the standard input message in the infinite loop, unless the user enters the Exit message.
🌐
Programiz
programiz.com › python-programming › input-output-import
Python Basic Input and Output (With Examples)
In this tutorial, we will learn simple ways to display output to users and take input from users in Python with the help of examples.
🌐
Python Course
python-course.eu › sys_module.php
1. sys-Module | Applications | python-course.eu
The standard input (stdin) is normally connected to the keyboard, while the standard error and standard output go to the terminal (or window) in which you are working. These data streams can be accessed from Python via the objects of the sys module with the same names, i.e. sys.stdin, sys.stdout ...
🌐
Linuxvoice
linuxvoice.com › code-ninja-stdin-and-stdout
Code Ninja: Stdin and stdout | Linux Voice
November 4, 2015 - We’ll do this using the argparser module (new in Python 2.7, so this won’t work if you’re using an older version). Using this, you only have to specify what options you have, and the module will take care of parsing them and putting them in variables so you can access them: from __future__ import print_function import argparse, sys parser = argparse.ArgumentParser(description=’echoing to stdout and stdin’) parser.add_argument(‘echo’, help=’The stuff to print on screen’) parser.add_argument(“--stderr”, help=”print the stuff to stderr (otherwise print to stdout)”, action=”store_true”) args = parser.parse_args() if args.stderr: print(args.echo, file=sys.stderr) else: print(args.echo)
🌐
SearchHounds
diveintopython.net › scripts_and_streams › stdin_stdout_stderr.html
Osteria Francescana: Italy's Modern Classic | SearchHounds
Located in the heart of Modena, Italy, Osteria Francescana represents Chef Massimo Bottura's innovative vision of Italian cuisine. Each dish tells a story, blending traditional flavors with contemporary artistry and technical mastery.
🌐
GitHub
gist.github.com › fyears › 4161739
python stdin example · GitHub
to use this I would run it like this $ echo "Your Text or Cat the file whatever" | python solnput.py <<o/p>> ... Concrete example where data flows into script using stdin from some previous program (another script). Line by line explanations for code below can be found here · # stdin.py sys.stdout = fsock print("15\n" "A\n" "B\n" "C\n" "D\n" "E\n" "F\n" "G\n" "H") sys.stdout = saveout fsock.close()
🌐
Quora
quora.com › How-do-I-take-input-from-STDIN-in-Python
How to take input from STDIN in Python - Quora
Answer (1 of 19): In python - you can use * sys.stdin : A file like object which is the standard input * sys.stdout : A file like object which is the standard output They are already open when you start your application - they don’t need to be opened or closed. To read the entirety of the inp...
🌐
Stack Overflow
stackoverrun.com › it › q › 2338341
Loading...
Catalogo di domande e risposte sulla programmazione