Skip to main content

How to Read What the User Types in Python with input

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A terminal program talks to its user with input(): it prints a question, waits for the user to type an answer and press Enter, and gives you that answer as text. This guide asks for a name and greets it, with real output from Python 3.15.2.

Syntax​

Syntax
<answer> = input(<prompt>)
PartWhat it means
<prompt>Optional text printed before the cursor, such as "What is your name? "
<answer>What the user typed, as a string, without the Enter at the end

Python's documentation: "If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, EOFError is raised."

Prerequisites​

  • uv and Python. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

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

The ask_name.py script in a macOS-style code window: input asks What is your name? and the answer is printed in a greeting.

The prompt ends with a space, so the answer doesn't touch the question mark.

Step 2: Run It​

Terminal
uv run python ask_name.py

Type Sam and press Enter:

Output
What is your name? Sam
Hello, Sam!

The output in a terminal: What is your name? followed by the typed answer Sam, then Hello, Sam!

The program stopped at the prompt until the answer arrived, then carried on with it in name.

Common Mistakes​

MistakeWhat happensDo this instead
Doing arithmetic with the answer, as in input("Age: ") + 1TypeError: can only concatenate str (not "int") to str: the answer is always a stringConvert it first: int(input("Age: ")) + 1
Leaving out the space at the end of the promptThe answer is glued to the questionEnd the prompt with a space

Troubleshooting​

SymptomLikely causeFix
EOFErrorInput ended before a line was read: Ctrl+D (Ctrl+Z then Enter on Windows), or a pipe with no more dataCatch EOFError where the program reads input
The program seems frozenIt's waiting at input() for an answerType an answer and press Enter

FAQ​

How do I get a number?​

Wrap the call in int() or float(): int(input("Age: ")). An answer that isn't a number then raises ValueError.

Can I read several answers?​

Call input() once per answer, or in a loop until the user types a word that ends it.

Conclusion​

input("What is your name? ") printed the question, waited for Sam and Enter, and returned "Sam", which the next line put into a greeting.

Additional Resources​

  • input() — prompts, return values and EOFError.