Python input() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Built-in

Definition

In Python, input(prompt) writes the optional prompt to the console, waits for the user to type a line and press Enter, and returns that line as a str without the trailing newline.

The result is always text, even if the user types digits, so convert it with int() or float() when you need a number. If the input stream ends (Ctrl+D on Linux and macOS, Ctrl+Z then Enter on Windows, or an empty pipe), input() raises EOFError.

input() is a built-in function: it lives in the builtins module, so it is always available without an import.

Syntax

Show a prompt and keep the returned text.

python
answer = input("Your name: ")
CallWhat it does
input()Waits for a line with no prompt.
input("Name: ")Prints Name: and waits on the same line.
int(input("Age: "))Reads text and converts it to an int.
input().split()Splits one line into words.

In the sample outputs below, (user types: ...) marks what was typed at the keyboard; it is not printed by the program.

Library declarations
python
input(prompt="", /)
# returns str; raises EOFError at end of input

Parameters

ParameterDescription
promptOptional. Text written to standard output before reading, with no newline added.

Return Value

Return ValueDescription
strThe line the user typed, without the trailing newline.

Python Examples

Example 1 — Ask for a Name and Greet the User

A sample run where the user types Ada and presses Enter.

python
name = input("Your name: ")
print(f"Hello, {name}!")
print("Characters:", len(name))

How It Works

  1. Prompt on the same line. input() prints Your name: without a newline, so the typing appears right after it.
  2. Enter ends the line. The returned string is "Ada"; the newline from Enter is removed.
  3. Use it like any str. The f-string greets the user, and len() counts the 3 characters.

Example 2 — Read Two Numbers and Add Them

A sample run where the user types 7 and then 5.

python
raw = input("First number: ")
print("type before converting:", type(raw).__name__)
first = int(raw)
second = int(input("Second number: "))
print(f"{first} + {second} = {first + second}")

How It Works

  1. Always text. Even though the user typed a digit, type(raw).__name__ is str.
  2. Convert explicitly. int(raw) turns "7" into the number 7.
  3. Inline conversion. int(input(...)) reads and converts the second value in one line.
  4. Real arithmetic. 7 + 5 gives 12; without int(), "7" + "5" would give "75".

Example 3 — Validate Input and Handle End of Input

A sample run where the user types abc, is asked again, and then ends input with Ctrl+D (Ctrl+Z then Enter on Windows).

python
def ask_age(prompt):
    while True:
        try:
            text = input(prompt)
        except EOFError:
            print()
            print("No more input; using the default age 18")
            return 18
        if text.strip().isdigit():
            return int(text)
        print("Please type a whole number.")


age = ask_age("Age: ")
print("Age recorded:", age)

How It Works

  1. Loop until valid. while True keeps asking until the text is all digits, then returns it as an int.
  2. Reject bad text. "abc" fails isdigit(), so the program explains and asks again.
  3. End of input. Ctrl+D (or an exhausted pipe) makes input() raise EOFError instead of returning.
  4. Graceful fallback. The except block moves to a new line, reports the default, and returns 18.

Frequently Asked Questions

Always str. Convert with int() or float() when you need a number.
Wrap it: age = int(input("Age: ")). Use try/except ValueError to handle text that is not a number.
Split the line: a, b = input().split(), then convert each part, for example map(int, input().split()).
The input stream ended before a line was read, for example Ctrl+D, Ctrl+Z on Windows, or running the script with no stdin. Catch EOFError to handle it.
No. eval() runs any Python code. Parse the text with int(), float(), or ast.literal_eval() instead.

Next: int()

Convert a number or string to an integer.

int() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful