input()
Read one line from stdin. Always str — cast if you need a number. Not for interactive apps.
Common call
name = input("Your name: ")
Returns
str — always
Replaces
the Python 2 `raw_input()` (Python 2 `input` evaluated the expression — a security hazard, now removed)
Watch out
always returns str; cast with int()/float() to get a number; raises EOFError at end-of-input
input(promptprompt — Optional prompt written to stdout without a trailing newline before reading. Empty string (default) skips the prompt.type: str · default: ""='')
→ str
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| prompt | str | no ("") | Optional prompt written to stdout without a trailing newline before reading. Empty string (default) skips the prompt. |
Return value
str — The user's input as a string. Reads until newline; the trailing newline is stripped. ALWAYS returns str — even if the user types a number. Cast with int() / float() if you need a number.
Common patterns
Get a string with a prompt
The everyday use.
name = input("Your name: ")
Get a number by casting
input never returns a number — always cast.
age = int(input("Age: "))
Validate with try/except
Casting can raise ValueError — handle gracefully.
try: age = int(input("Age: ")) except ValueError: print("must be a number")
Case-insensitive yes/no check
Casefold before comparing to avoid case bugs.
if input("Continue? ").strip().casefold() in ("y", "yes"): ...
Prefer argparse for scripts
CLI args are more testable than interactive prompts.
# argparse for tools; input() is for interactive prototypesExamples
1. Basic
input("Name: ")
Returns
"Alice" # whatever the user typed2. Always str
x = input("Number: ")
type(x)
Returns
<class 'str'> # even if they typed 423. Cast to int
age = int(input("Age: "))
Returns
424. Cast to float
pi = float(input("π: "))
Returns
3.145. Blank input
input("Anything: ")
Returns
"" # empty string6. End of input
input() # at EOF
Returns
EOFErrorPitfalls
1. input() ALWAYS returns str
The single most common input() bug. Even if the user types a number, you get the string form. Arithmetic on the result raises TypeError until you cast.
String math
x = input("Age: ") x + 1
TypeError: can only concatenate str (not "int") to str
Cast to int
x = int(input("Age: ")) x + 1
43
2. The int() cast raises ValueError on non-numeric input
A user typing "forty-two" instead of "42" will crash your program if int() is unguarded. Wrap in try/except or validate first.
Uncaught error
int(input("Age: ")) # user types "abc"
ValueError: invalid literal for int() with base 10: 'abc'
Guarded cast
try: age = int(input("Age: ")) except ValueError: ...
handled
3. EOFError at end of input
When stdin runs out (piped input finished, Ctrl+D pressed), input() raises EOFError. In loops that read until quit, catch it or check for a specific sentinel.
Uncaught EOF
while True: line = input()
EOFError after stdin closes
Try/except
try: while True: line = input() except EOFError: ...
clean exit
4. Python 2 input() vs raw_input()
Python 2 had TWO functions: input() (which called eval() — dangerous!) and raw_input() (which returned a string). Python 3 renamed raw_input to input and removed the eval-based version. If porting old Python 2 code, watch for input() calls that assumed evaluation.
Assumed eval
x = input("expr: ") # Python 2: evaluated
silently different in Python 3
Explicit eval
x = eval(input("expr: ")) # only if safe!
clear intent
When to use
Use it
- Interactive prompts in scripts and prototypes
- REPL-like tools where the user types responses
- Quick one-off "ask the user" questions
- Learning material — input is the canonical "get user data" teaching tool
Reach for something else
- Command-line arguments → argparse
- GUI applications → the GUI framework's dialogs
- Web apps → the HTTP request
- Batch processing → read from a file or stdin non-interactively
Notes
Complexity
O(n) in the length of the line
Return
A string — always
CPython impl
Python/bltinmodule.c :: builtin_input — reads from stdin, strips trailing newline
Memory
Allocates one string
Thread-safe
Depends on the underlying stdin; not usually a concern in typical scripts
FAQ
Because input() returns a string. `"42" + 1` is a type error. Cast with `int(input(...))` or `float(input(...))` if you need a number.
History
1.0
input() and raw_input() both present in Python 1.0.
3.0
raw_input renamed to input; the eval-based input removed for security.