Python 3 reference · Built-in Functions

Python input() Function

Read a line of text typed by the user, after printing an optional prompt. The result is always a string.

Python 3 Since 3.0

Syntax

input(prompt="")

Parameters

ParameterDescription
prompt optional Text shown before the cursor. Default: ""

Return value

The line as a str, without the trailing newline.

Examples

Ask for a nameTry it yourself
import io, sys
sys.stdin = io.StringIO("Ada\n")  # pretend the user typed "Ada"

name = input("Your name: ")
print(f"Hello, {name}!")
sys.stdin = sys.__stdin__  # restore real input

Output

Your name: Hello, Ada!
Read a numberTry it yourself
import io, sys
sys.stdin = io.StringIO("41\n")

age = int(input("Age: "))  # convert the text to a number
print("Next year you'll be", age + 1)
sys.stdin = sys.__stdin__

Output

Age: Next year you'll be 42

Common mistakes

  • input() always returns a string: input() + 1 fails. Convert with int() or float(), and catch ValueError for bad input.
  • When input ends (Ctrl-D, or a closed pipe), input() raises EOFError.
  • The examples replace sys.stdin so they can run here without a keyboard.

Version history

VersionChange
Python 3.0Added