Can anyone give me a quick tutorial in stdin and stdout in Python 3?
Solution 1
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.
Solution 2
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
.
Solution 3
Is
input()
thestdin
function in Python 3?
Yes.
Does that mean that when you open your
filename.py
program, thestdin
is what the user types?
Yes.
Is
print()
thestdout
function in Python 3, or do you have to write to a file?
You can use either.
For the Spotify puzzle, is says "Input is read from stdin". What should my file include of
stdin
andstdout
?
Just use import sys
to get to the sys.stdin
and sys.stdout
files. You don't need the import if you use the input
and print
built-in functions instead.
Solution 4
Is input() the stdin function in Python 3? Does that mean that when you open your filename.py program, the stdin is what the user types?
input()
reads from the standard input (called stdin
for short in some contexts), yes. No language that I know of has a function named stdin
in the standard library. When you run the program in the usual way, what the user types is supplied to standard input, yes. That's what "standard input" means. There is a separate program that is feeding what the user types to standard input (and doing a couple of other convenient things like interpreting the backspace key). Your script is not what creates the window that text is typed into.
sys.stdin
(i.e., the value stdin
defined in the sys
module) is an object that represents the standard input. This is provided so that you can use it in contexts where you're expected to specify "a file", to cause input to be read from the user instead of a file.
Is print() the stdout function in Python 3, or do you have to write to a file?
print()
writes (by default; you can supply a file
keyword argument to change this) to the standard output (called stdout
for short in some contexts), yes. All the above caveats apply: stdout
isn't a function in any language I've ever heard of, and a completely separate program is actually causing the output of your script to be displayed on screen in a pretty 80x24 white-text-on-black-background (or however you have it configured) box.
sys.stdout
is an object that represents the standard output, used similarly to sys.stdin
. You can use it explicitly with the print
function, but there's no point: it's the default.
For the Spotify puzzle, is says "Input is read from stdin". What should my file include of stdin and stdout?
The problem specification means "use the input()
function to receive input".
Martin Hallén
A computer science student at Norwegian University of Science and Technology (NTNU).
Updated on July 09, 2022Comments
-
Martin Hallén almost 2 years
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()
thestdin
function in Python 3? Does that mean that when you open your filename.py program, thestdin
is what the user types? - Is
print()
thestdout
function in Python 3, or do you have to write to a file? - For the Spotify puzzle, is says "Input is read from stdin". What should my file include of
stdin
andstdout
?
Update: Does that mean that i can use:
import sys unfmtdDate = str(sys.stdin.read())
...instead of...
unfmtdDate = str(input())
?
- Is
-
Greg Hewgill over 12 years
stout
?stein
? Have another beer! -
Karl Knechtel over 12 yearsNote that
input()
cannot read from a specified file (there are ways to cause the standard input to be a file, but again, that's generally external to your program). It's not meant to be exactly symmetric with theprint()
function. After all, it displays a prompt... wouldn't make sense to display a prompt while reading from a file, see? -
Mike DeSimone about 12 years@Greg Hewgill: My browser seems to have grown itself an autocorrect future. It's being annoying.