Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Python’s built-in input() function displays an optional prompt, reads one line of input, and returns the response as a string. Use the string directly for text, or convert it with int() or float() before doing numeric calculations.

What does input() do in Python?

input() reads a line from the program’s input stream. If you give it a prompt, Python writes that prompt to standard output without adding a newline first. After reading the line, it removes the trailing newline and returns the remaining text as a string. Because it is a built-in function, you do not need to import it. See the Python 3.12 built-in functions reference.

Display a prompt and read text

Assign the returned string to a variable, then use it as text:

name = input("What is your name? ")
print(f"Hello, {name}!")

The prompt’s trailing space is intentional: it leaves a gap between the question and the response the user types. The function waits for a line of input; pressing Enter submits that line.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why does input() return a string?

The function returns the characters the user entered as text. It does not infer whether those characters represent a number, a date, or another kind of value. For example, if the user types 42, the result is the string "42", not the integer 42. You can display or join that text, but you must convert it before doing arithmetic.

How do you convert input to a number?

Choose a conversion that matches the kind of value your program needs. The built-in functions reference documents int() and numeric string conversion.

Conversion Use it for Example
int() Whole-number values int("42") returns 42
float() Values that may include a decimal fraction float("3.5") returns 3.5

Convert a response to an integer

Read the response, convert it, and then perform the calculation:

age_text = input("How old are you? ")
age = int(age_text)
print(f"Next year you will be {age + 1}.")

For a decimal value, use float() instead:

price_text = input("Enter a price: ")
price = float(price_text)
print(price + 1.0)

Handle text that cannot be converted

If the response is not in a format the conversion accepts, int() or float() raises a conversion error. For a user-facing program, catch that error and explain the expected input format rather than letting the program stop with an unhandled traceback:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
age_text = input("How old are you? ")

try:
    age = int(age_text)
except ValueError:
    print("Enter your age as a whole number.")
else:
    print(f"Next year you will be {age + 1}.")
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What does EOFError mean with input()?

input() raises EOFError when the input stream reaches its end before a line can be read. This may happen in a noninteractive run when no input is supplied. Python documents the exception in its built-in exceptions reference.

If end-of-file is an expected possibility in your program, catch the exception and decide how to proceed—for example, display a message or exit cleanly:

try:
    name = input("What is your name? ")
except EOFError:
    print("No input was available.")
else:
    print(f"Hello, {name}!")

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.