🌐
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

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
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
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
Writing to an actively running Python script's STDIN
Are you on Linux or Mac? Then what about this ? (The question is about screen, but not all the answers are.) More on reddit.com
🌐 r/learnpython
4
3
July 20, 2022
🌐
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.

🌐
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.
🌐
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]))'
🌐
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.
🌐
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.
🌐
Reddit
reddit.com › r/learnpython › writing to an actively running python script's stdin
r/learnpython on Reddit: Writing to an actively running Python script's STDIN
July 20, 2022 -

I have a python script that takes in input string form the user, and returns an output string (it's a bot). But, I want to turn this script into a 'service' of sorts, and therefore I want an external app to write to to a running version of the script (with an active PID) and receive a response, as though a user were typing the input at the CLI. Here is the script:

while True:
try:
user_input = input('You: ')
bot_response = bot.get_response(user_input)
print('Bot:', bot_response, bot_response.confidence)

How can I send a string to this script and receive a response. Note: I don't want to put a wrapper around the script, because the script has some import statements in the header that take a while to load. Once the script loads the necessary libraries, it's pretty speedy, but the loading process makes the script too slow to function with a wrapper.

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