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

Preferred method of reading from stdin?
The more pythonesque way is to use input() if you are accepting text from a human and use sys.stdin if you are reading a lot of redirected text, say from a pipe. More on reddit.com
🌐 r/learnpython
6
3
July 21, 2023
Can anyone give me a quick tutorial in stdin and stdout in Python 3? - Stack Overflow
I know this sounds like something I can google, but the truth is that I don't find or do not understand what the very few Python 3 sources explains. So here are my questions: Is input() the stdin More on stackoverflow.com
🌐 stackoverflow.com
python - How do I read from stdin? - Stack Overflow
Python also has built-in functions ... See the Python documentation under Built-in Functions. ... This reads a single line, which isn't really what the OP asked about. I interpret the question as "how do I read a bunch of lines from an open file handle until EOF?" 2015-12-22T08:51:49.37Z+00:00 ... The OP isn't asking to read input from a keyboard, He is asking to read from stdin which in a ... More on stackoverflow.com
🌐 stackoverflow.com
What's the difference between sys.stdin.read() and input()?
Brendan Whiting is having issues with: We have both in this video and I'm not sure what the difference is. More on teamtreehouse.com
🌐 teamtreehouse.com
2
November 27, 2015
🌐
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.
🌐
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
🌐
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.
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.
🌐
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.
🌐
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.
🌐
CodersLegacy
coderslegacy.com › home › python › python stdin – taking input
Python stdin - Taking input - CodersLegacy
October 6, 2022 - Besides the input() function, another input method is the Python stdin, which is a part of the sys module. You can use this to take input....
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.