🌐
Delft Stack
delftstack.com › home › howto › python › how to read input from stdin in python
How to Read Input From stdin in Python | Delft Stack
March 11, 2025 - Discover how to read input from stdin in Python with various methods, including the input function, reading multiple lines, file input, and command line arguments. This comprehensive guide provides clear examples and detailed explanations to enhance your coding skills.
🌐
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.
🌐
Certiquizz
certiquizz.com › accueil › cours › programmation › python › python pour développeurs : le guide complet et avancé › redirection des entrées/sorties
Redirection des entrées/sorties en Python : stdin, stdout, stderr et fichiers
June 1, 2025 - `<` : Redirige l'entrée standard (stdin) depuis un fichier. `2>` : Redirige la sortie d'erreur standard (stderr) vers un fichier. `2>&1` : Redirige stderr vers le même endroit que stdout.
🌐
Developpez
python.developpez.com › cours › DiveIntoPython › php › frdiveintopython › scripts_and_streams › stdin_stdout_stderr.php
10.2. Entrée, sortie et erreur standard - Dive Into Python
[you@localhost kgp]$ python kgp.py -g binary.xml 01100111 [you@localhost kgp]$ cat binary.xml <?xml version="1.0"?> <!DOCTYPE grammar PUBLIC "-//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 · Comment donc notre script «sait»-il qu'il doit lire à partir de l'entrée standard quand le fichier de grammaire correspond à «-» ? Cela n'a rien de magique; juste logique. 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 ...]
🌐
DigitalOcean
digitalocean.com › community › tutorials › read-stdin-python
How to Read from stdin in Python | DigitalOcean
August 3, 2022 - Python sys module stdin is used by the interpreter for standard input. Internally, it calls the input() function. The input string is appended with a newline character (\n) in the end. So, you can use the rstrip() function to remove it. Here is a simple program to read user messages from the ...
Find elsewhere
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.

🌐
GitHub
gist.github.com › fyears › 4161739
python stdin example · GitHub
python3 -c 'import sys; from urllib.parse import quote; print(quote(sys.stdin.readlines()[0]))'
🌐
Python Guides
pythonguides.com › python-stderr-stdin-and-stdout
Python – stdin stdout and stderr [6 Examples] - Python Guides
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.
🌐
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
🌐
Sentry
sentry.io › sentry answers › python › read user input (stdin) in python
Read user input (STDIN) in Python | Sentry
November 15, 2023 - If we run our script without piped input, it will wait for us to enter something into stdin. We can then type or paste in multiple lines, finishing with Ctrl-D: $ python stdin_reader.py Many years later, as he faced the firing squad, Colonel Aureliano Buendía was to remember that distant afternoon when his father took him to discover ice.
🌐
Runebook.dev
runebook.dev › en › docs › python › library › sys › sys.__stdin__
python - Understanding and Restoring sys.stdin with sys.stdin
October 21, 2025 - In Python, the sys module provides access to system-specific parameters and functions. The objects sys.stdin, sys.stdout, and sys.stderr represent the standard I/O streams for input, output, and errors, respectively.
🌐
Python
docs.python.org › 3 › library › sys.html
sys — System-specific parameters and functions
Non-console character devices such as NUL (i.e. where isatty() returns True) use the value of the console input and output codepages at startup, respectively for stdin and stdout/stderr. This defaults to the system locale encoding if the process is not initially attached to a console. The special behaviour of the console can be overridden by setting the environment variable PYTHONLEGACYWINDOWSSTDIO before starting Python.
🌐
Quora
quora.com › How-do-I-take-input-from-STDIN-in-Python
How to take input from STDIN in Python - Quora
Hence we cannot use raw_input() , input(), as they reads input on run-time. But stdin (or standard input) in Python behaves like a file, and you have to read the data from stdin, rather than reading input in run time.
Top answer
1 of 2
15
Hi Brendan, in this video the "sys.stdin.read()" is described as being able to take a newline and finish your entry with Control+D. input() would finish your entry with the "Enter" key being pressed on your keyboard, so you couldn't include a newline in your data input that way.
2 of 2
6
That sounds roughly correct, however input() also takes as an argument a string to use as a prompt, while sys.stdin.read() takes the length to read into the user-entered string as an optional argument instead (and provides no prompt - in the video, a print() was provided to serve as a prompt instead). For more information on what these functions are doing though, you can use help(sys.stdin.read) and help(input) while in a Python interpreter, or visit https://docs.python.org/2/library/sys.html for more information about the sys library and its methods, including stdin. As for your other question, we have to import the sys library because sys.stdin.read() is reflecting a method that exists only in that library. The reason it's so long is that we just imported the library, so we have to reference sys at the beginning of any function from that library, then .stdin() is a function with a .read() method available in it (among others) - so it wouldn't make sense to just say read() without telling Python which read() method you're asking it to use (other functions, including one you write yourself, could include their own read() methods). If you mean to say why sys is a library instead of being ready for use in Python all the time, that's likely because it would be inefficient for Python to keep libraries loaded if they aren't being used, so the library is kept optional.