stdin (standard input) is where a program reads data from the keyboard or from another program or file through a pipe.
input() for interactive input
# Prompt the user for their name
name = input("Enter your name: ")
print(f"Hello, {name}!")

input() reads one line and returns it as a string. Convert it for numbers: age = int(input("Age: ")).
sys.stdin for file-like input
sys.stdin works like a file object.
import sys
print("Enter multiple lines (Ctrl+D to end):")
# Read all lines from stdin
lines = sys.stdin.readlines()
print("You entered:")
print("".join(lines))

Press Ctrl+D (Linux and macOS) or Ctrl+Z then Enter (Windows) to end the input.
Read everything at once
import sys
data = sys.stdin.read()
print(f"Characters: {len(data)}")

Read line by line
This keeps memory use low for big input:
import sys
for line in sys.stdin:
print(line.strip().upper())
Read piped data
Save a script as upper.py with the loop above. Then run:
echo "hello world" | python3 upper.py

or read from a file:
python3 upper.py < notes.txt
fileinput
fileinput reads files named on the command line, or stdin if there are none:
import fileinput
for line in fileinput.input():
print(line.strip())
Which to use
| Need | Use |
|---|---|
| One value from the user | input() |
| All text | sys.stdin.read() |
| Big input, line by line | for line in sys.stdin |
| Files or stdin | fileinput |
Quick recap
input()for a prompt,sys.stdinfor piped or long input.