input()
Shows an optional prompt, waits for the user to type a line and press Enter, and returns what they typed as a string.
input(prompt='')input() pauses your program and waits for the user to type something. When they press Enter, it gives back the typed line as a string, without the Enter at the end.
Give it a prompt, such as input("Your name: "), so the user knows what to type. The prompt is written without a new line, so the answer appears right after it. End the prompt with a space to keep it readable.
The answer is always a string, even when the user types a number. input() of 5 gives "5", and "5" + "3" is "53". Convert it with int() or float() before doing maths.
Parameters
| Parameter | What it is |
|---|---|
promptoptional, default '' | Text shown before the cursor. It is written exactly as given, with no space or new line added. |
Return value
A string with the line the user typed, without the final new line. An empty line gives "".
Examples
1Ask for a name
In these examples, the text the user types is given to the program for you. The screen shows the prompt, then what the program prints.
name = input("What is your name? ")
print("Hello,", name)What is your name? Hello, Amara
On a real screen the typed name appears after the prompt and the greeting goes on the next line.
2Turn the answer into a number
age = int(input("How old are you? "))
print("Next year you will be", age + 1)How old are you? Next year you will be 30
3Read two numbers from one line
a, b = input("Two numbers: ").split()
print("Sum:", int(a) + int(b))Two numbers: Sum: 11
str.split() cuts the line at the space. With many numbers, list(map(int, input().split())) converts them all at once.
4Keep asking until the answer is valid
while True:
text = input("Quantity: ")
if text.isdigit():
break
print("Please type a whole number.")
print("You ordered", int(text))Quantity: Please type a whole number. Quantity: You ordered 3
5Clean up yes/no answers
answer = input("Save changes? (y/n) ").strip().lower()
if answer in ("y", "yes"):
print("Saved.")
else:
print("Not saved.")Save changes? (y/n) Saved.
strip() removes stray spaces and lower() makes Yes, YES and yes all match.
Common mistakes
Doing maths with the raw answer
input() returns text, so + joins the two strings instead of adding the numbers. Convert first.
a = input("First: ")
b = input("Second: ")
print(a + b)
print(int(a) + int(b))First: Second: 53 8
Crashing on a bad number
int("ten") raises ValueError. Wrap the conversion in try / except and ask again or use a default.
text = input("Copies: ")
try:
copies = int(text)
except ValueError:
print("Not a number, using 1")
copies = 1
print("Printing", copies, "copies")Copies: Not a number, using 1 Printing 1 copies
Questions people ask
How do I get a number from input in Python?
Wrap it: int(input("Age: ")) for whole numbers, float(input("Price: ")) for decimals.
How do I read several values on one line?
Split the line: x, y = input().split(), or numbers = list(map(int, input().split())) for a list of whole numbers.
What is raw_input()?
It was Python 2's name for what Python 3 calls input(). Python 3 has no raw_input().
Why do I get EOFError?
The program asked for input but there was nothing left to read, for example when it runs without a keyboard attached. Catch EOFError if your program may run that way.