input()
input([prompt]) Returns:
str · Added in v3.0 · Updated March 13, 2026 · Built-in Functions built-in input console io
The input() function reads a single line from standard input (typically the keyboard) and returns it as a string, excluding the trailing newline. If a prompt string is provided, it is displayed to the user before waiting for input. This function is essential for creating interactive command-line programs that communicate with users through the console.
Syntax
input([prompt])
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
prompt | str | None | The string printed to standard output before reading input |
Examples
Basic usage
name = input()
print(f"Hello, {name}!")
With a prompt
age = input("Enter your age: ")
print(f"You are {age} years old")
Handling EOF
When input reaches end-of-file (Ctrl+D on Unix, Ctrl+Z on Windows), input() raises EOFError:
try:
data = input()
print(f"Received: {data}")
except EOFError:
print("No more input available")
Common Patterns
Converting input to other types
age = int(input("Enter your age: "))
price = float(input("Enter price: "))
is_active = bool(input("Active? (yes/no): ").lower() == "yes")
Reading from a file interactively
while True:
try:
command = input(">>> ")
if command.lower() in ("exit", "quit"):
break
print(f"Executing: {command}")
except EOFError:
print("\nGoodbye!")
break
Validating input with a loop
while True:
response = input("Do you want to continue? (yes/no): ").lower().strip()
if response in ("yes", "no"):
break
print("Please answer yes or no")
Safe numeric input
def get_integer(prompt):
while True:
try:
return int(input(prompt))
except ValueError:
print("Please enter a valid integer")
def get_float(prompt):
while True:
try:
return float(input(prompt))
except ValueError:
print("Please enter a valid number")
age = get_integer("Enter your age: ")
price = get_float("Enter price: ")
Errors
- EOFError — Raised when input reaches end-of-file without any data
- KeyboardInterrupt — Raised when the user interrupts with Ctrl+C
nThe input() function is synchronous and blocks execution until the user provides input. This behavior is important to understand when building interactive applications. In Python 2, the equivalent function was called raw_input().