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
| Parameter | Description |
|---|---|
prompt optional |
Text shown before the cursor. Default: "" |
Return value
The line as a str, without the trailing newline.
Examples
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!
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() + 1fails. Convert withint()orfloat(), and catchValueErrorfor bad input.- When input ends (Ctrl-D, or a closed pipe),
input()raisesEOFError. - The examples replace
sys.stdinso they can run here without a keyboard.
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
print()· Write values to standard output (or a file), separated by spaces and followed by a newline. Both are configurable.int()· Convert a number or string to an integer. Floats are truncated toward zero, and strings can be in any base from 2 to 36.str()· Convert a value to its readable text form, or decode bytes into a string. Strings are immutable sequences of characters.- Python Get Started · tutorial