How to get user input in Python
Call input('prompt: '). It blocks and returns the user's line as a str (never a number).
name = input("What's your name? ")
age = input("How old are you? ")
print(f"Hi {name}, you'll be {int(age) + 1} next year.")input() blocks until the user types something and hits Enter, then returns the line as a string (no trailing newline). If you're expecting a number, cast with int() or float() and handle ValueError for garbage input.
For scripts that will be piped or run non-interactively (cron, CI, systemd), input() reads from stdin instead of a terminal β same shape, just no visible prompt. If you specifically need a terminal, use sys.stdin.isatty() to detect.
For passwords or anything sensitive, use getpass.getpass() β it doesn't echo the characters. Never store the input in a variable that ends up in a log.
Variations
Parse as a number
raw = input("Age: ")
try:
age = int(raw)
except ValueError:
print("Please enter a whole number.")int() strips whitespace but rejects any non-digit.
Password (no echo)
from getpass import getpass
pw = getpass("Password: ")Terminal-only. In non-terminal contexts falls back to plain input().
Multi-line input
print("Enter text (Ctrl+D to finish):")
text = sys.stdin.read()sys.stdin.read() consumes until EOF (Ctrl+D on Unix, Ctrl+Z Enter on Windows).
Get one Python lesson + one career idea every Friday
No spam, no "buy our course now". Three bullets, every Friday. Unsubscribe with one click.