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 sysunfmtdDate = str(sys.stdin.read())
...instead of...
unfmtdDate = str(input())
?
Best Answer
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.
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 sysdata = sys.stdin.read()
After these two lines the input for your program is now in the str data
.
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.
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".
From the Python documentation for print
:
The file argument must be an object with a
write(string)
method; if it is not present orNone
,sys.stdout
will be used. Output buffering is determined by file. Usefile.flush()
to ensure, for instance, immediate appearance on a screen.