Built-in function

input()

Shows an optional prompt, waits for the user to type a line and press Enter, and returns what they typed as a string.

BeginnerEvery example below runs in your browser. Change the code and press Run.

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

ParameterWhat it is
prompt
optional, 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.

Pythoneditable
name = input("What is your name? ")
print("Hello,", name)
Output
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

Pythoneditable
age = int(input("How old are you? "))
print("Next year you will be", age + 1)
Output
How old are you? Next year you will be 30

3Read two numbers from one line

Pythoneditable
a, b = input("Two numbers: ").split()
print("Sum:", int(a) + int(b))
Output
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

Pythoneditable
while True:
    text = input("Quantity: ")
    if text.isdigit():
        break
    print("Please type a whole number.")
print("You ordered", int(text))
Output
Quantity: Please type a whole number.
Quantity: You ordered 3

5Clean up yes/no answers

Pythoneditable
answer = input("Save changes? (y/n) ").strip().lower()
if answer in ("y", "yes"):
    print("Saved.")
else:
    print("Not saved.")
Output
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.

Pythoneditable
a = input("First: ")
b = input("Second: ")
print(a + b)
print(int(a) + int(b))
Output
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.

Pythoneditable
text = input("Copies: ")
try:
    copies = int(text)
except ValueError:
    print("Not a number, using 1")
    copies = 1
print("Printing", copies, "copies")
Output
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.

Spotted a mistake on this page? Tell us