Python Reference
input()
Read user input from the console. Always returns a string.
Last updated: October 2026
Definition: input(prompt) reads a line from standard input and returns it as a string. It optionally prints a prompt first. The trailing newline is stripped.
🎬 Live Example
Five common input patterns. Click any button to see the output.
💡 input() always returns str. Convert types explicitly.
Basic Syntax
input(prompt)
# prompt — optional string shown before reading
# Returns: str (always)
# Strips the trailing newline
# Raises EOFError on end-of-file
🎯 Parameters
| Parameter | Type | Description |
|---|---|---|
prompt | str | Optional. Shown before reading. No newline added. |
Basic Input
name = input()
# User types: Alice
# name = 'Alice'
# Always a string
age = input()
# User types: 30
# age = '30' (str, not int!)
With Prompt
name = input("Enter your name: ")
# Displays: Enter your name:
# Then waits for input
age = input("Age: ")
# Displays: Age:
Pro tip: The prompt is printed to stdout without a newline, so the cursor stays on the same line.
Convert to Integer or Float
# ✅ Convert immediately
age = int(input("Age: "))
price = float(input("Price: "))
# ❌ This compares a string to an int — always False
age = input("Age: ")
if age > 18: # TypeError in Python 3!
...
Common bug: Forgetting to convert. input() always returns a string, so comparisons with numbers fail.
Read Multiple Values
# User types: 10 20
a, b = input().split()
# a='10', b='20' (both str)
# Convert to int
a, b = map(int, input().split())
# a=10, b=20
# Read a list of numbers
nums = list(map(int, input().split()))
Real-World: Safe Input Loop
while True:
try:
age = int(input("Enter age: "))
break
except ValueError:
print("Please enter a valid number.")
print(f"Your age is {age}")
# Handles non-numeric input gracefully
🎯 input vs sys.stdin vs file reading
| Method | Use when | Notes |
|---|---|---|
input() | Interactive prompts | Prints prompt, strips newline |
sys.stdin.readline() | No prompt needed | Keeps newline |
open().read() | Reading from files | Not user input |
⚠️ Common Mistakes
- Forgetting to convert.
input()always returns a string. Useint()orfloat(). - Not handling ValueError. Invalid numeric input raises an exception. Wrap in try/except.
- Assuming input includes newline. The newline is stripped automatically.
- Not handling EOFError in scripts. Reading from a pipe that closes raises EOFError.
- Using input() for passwords. It echoes to the terminal. Use
getpassinstead.
❓ Frequently Asked Questions
What does input() do in Python?
input() reads a line of text from the user and returns it as a string. It optionally prints a prompt first. The trailing newline is stripped.
How do I get an integer from input()?
input() always returns a string. Convert it: int(input('Enter a number: ')). Wrap in try/except to handle invalid input.
What does input() return on EOF?
input() raises EOFError if the user sends end-of-file (Ctrl+D on Unix, Ctrl+Z on Windows). Handle it with try/except for scripts reading from pipes.
Does input() include the newline?
No. input() strips the trailing newline character before returning the string.
How do I read multiple values on one line?
Use input().split(): a, b = input().split(). For integers: a, b = map(int, input().split()).