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()thestdinfunction in Python 3? Does that mean that when you open your filename.py program, thestdinis what the user types? - Is
print()thestdoutfunction 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
stdinandstdout?
Update:
Does that mean that i can use:
import sys
unfmtdDate = str(sys.stdin.read())
…instead of…
unfmtdDate = str(input())
?
stdinandstdoutare file-like objects provided by the OS. In general, when a program is run in an interactive session,stdinis keyboard input andstdoutis 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 thatstdinis redirected from a data file, and when the input format is given it’s usually best to usesys.stdin.read()rather than prompting for input withinput().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 tostdout. There’s no need to do anything special to specify the output stream.print()writes tosys.stdoutif no alternate file is given to it as afile=parameter.