In Python, input(prompt) writes the optional prompt to the console, waits for the user to type a line and press Enter, and returns that line as a str without the trailing newline.
The result is always text, even if the user types digits, so convert it with int() or float() when you need a number. If the input stream ends (Ctrl+D on Linux and macOS, Ctrl+Z then Enter on Windows, or an empty pipe), input() raises EOFError.
input() is a built-in function: it lives in the builtins module, so it is always available without an import.
Syntax
Show a prompt and keep the returned text.
python
answer = input("Your name: ")
Call
What it does
input()
Waits for a line with no prompt.
input("Name: ")
Prints Name: and waits on the same line.
int(input("Age: "))
Reads text and converts it to an int.
input().split()
Splits one line into words.
In the sample outputs below, (user types: ...) marks what was typed at the keyboard; it is not printed by the program.
Library declarations
python
input(prompt="", /)
# returns str; raises EOFError at end of input
Parameters
Parameter
Description
prompt
Optional. Text written to standard output before reading, with no newline added.
Return Value
Return Value
Description
str
The line the user typed, without the trailing newline.
Python Examples
Example 1 — Ask for a Name and Greet the User
A sample run where the user types Ada and presses Enter.
python
name = input("Your name: ")
print(f"Hello, {name}!")
print("Characters:", len(name))
Output
Your name: Ada (user types: Ada)
Hello, Ada!
Characters: 3
How It Works
Prompt on the same line.input() prints Your name: without a newline, so the typing appears right after it.
Enter ends the line. The returned string is "Ada"; the newline from Enter is removed.
Use it like any str. The f-string greets the user, and len() counts the 3 characters.
Example 2 — Read Two Numbers and Add Them
A sample run where the user types 7 and then 5.
python
raw = input("First number: ")
print("type before converting:", type(raw).__name__)
first = int(raw)
second = int(input("Second number: "))
print(f"{first} + {second} = {first + second}")
Output
First number: 7 (user types: 7)
type before converting: str
Second number: 5 (user types: 5)
7 + 5 = 12
How It Works
Always text. Even though the user typed a digit, type(raw).__name__ is str.
Convert explicitly.int(raw) turns "7" into the number 7.
Inline conversion.int(input(...)) reads and converts the second value in one line.
Real arithmetic. 7 + 5 gives 12; without int(), "7" + "5" would give "75".
Example 3 — Validate Input and Handle End of Input
A sample run where the user types abc, is asked again, and then ends input with Ctrl+D (Ctrl+Z then Enter on Windows).
python
def ask_age(prompt):
while True:
try:
text = input(prompt)
except EOFError:
print()
print("No more input; using the default age 18")
return 18
if text.strip().isdigit():
return int(text)
print("Please type a whole number.")
age = ask_age("Age: ")
print("Age recorded:", age)
Output
Age: abc (user types: abc)
Please type a whole number.
Age: (user types: Ctrl+D)
No more input; using the default age 18
Age recorded: 18
How It Works
Loop until valid.while True keeps asking until the text is all digits, then returns it as an int.
Reject bad text."abc" fails isdigit(), so the program explains and asks again.
End of input. Ctrl+D (or an exhausted pipe) makes input() raise EOFError instead of returning.
Graceful fallback. The except block moves to a new line, reports the default, and returns 18.
Frequently Asked Questions
Always str. Convert with int() or float() when you need a number.
Wrap it: age = int(input("Age: ")). Use try/except ValueError to handle text that is not a number.
Split the line: a, b = input().split(), then convert each part, for example map(int, input().split()).
The input stream ended before a line was read, for example Ctrl+D, Ctrl+Z on Windows, or running the script with no stdin. Catch EOFError to handle it.
No. eval() runs any Python code. Parse the text with int(), float(), or ast.literal_eval() instead.