A prompt in Python is simply a message that asks the user for input, usually written with the built-in input() function. The detail most people get wrong is that input() always returns a string, even when the user types a number. You must convert it yourself with int() or float() before doing math.

This page walks you through writing prompts, converting the answers, checking them, and keeping your programs calm and predictable for the person typing.

Write Your First Prompt with input()

The input() function pauses your program and waits for the user to type something and press Enter. Whatever they type comes back to you as a string. You can store it in a variable and use it later.

  1. Open a new Python file or your interactive shell.
  2. Type name = input("What is your name? ") and press Enter.
  3. The program waits. Type a name and press Enter.
  4. Type print("Hello, " + name) to see the result.

The space at the end of the prompt string matters. Without it, the user's typing appears jammed against the question mark. A trailing space keeps the cursor one step away, which feels natural.

You can also use a prompt with no text at all: input(). That works, but the user sees a blank line and may not know what to do. Always give a short, clear question.

Convert the Answer Before You Do Math

Here is the mistake that trips up most beginners. If you write age = input("Your age: ") and the user types 30, then age holds the text "30", not the number 30. Adding 1 to it with age + 1 raises a TypeError, because Python will not add a string and an integer.

Wrap the input in a converter. For whole numbers, use int():

age = int(input("Your age: "))

For decimals, use float():

height = float(input("Your height in meters: "))

Now age + 1 gives 31, and height * 2 gives a number you can print. This one habit prevents most early errors.

A simple greeting

Suppose you want to ask for a name and a favorite number, then double the number.

  1. name = input("What is your name? ")
  2. number = int(input("Pick a whole number: "))
  3. print("Hello, " + name + ". Your number doubled is " + str(number * 2) + ".")

If the user types Sam and 7, the output reads: Hello, Sam. Your number doubled is 14. Notice the str() around the result. You need it because you are joining a number onto a string.

Handle Bad Input Without Crashing

If the user types letters when you asked for a number, int() raises a ValueError and the program stops. That is frustrating. A loop lets you ask again until the answer is valid.

  1. Start a while True: loop.
  2. Inside, wrap the input and conversion in try:.
  3. If conversion succeeds, break out of the loop.
  4. If it fails, print a short message and let the loop repeat.

This pattern keeps the program running and gives the user another chance. Keep the message kind and specific: "Please type a whole number, like 5." A vague "Invalid input" leaves the person guessing.

You can also check a range. If you ask for a number between 1 and 10, test it after conversion and ask again if it falls outside. Combine the conversion check and the range check in the same loop so the user is not asked twice.

Make Prompts Clear and Kind to Read

A good prompt tells the user exactly what format you expect. Compare "Date?" with "Date (YYYY-MM-DD):". The second one removes guesswork. If you need a yes or no, say so: "Continue? (yes/no): ".

Keep prompts short. One question per prompt is easier to answer than three stacked together. If you need several pieces of information, ask in separate steps so the user can focus on each one.

Use a colon or a question mark consistently. Pick one style and stay with it, so the program feels predictable. Predictable prompts feel calm to use, even in a small script.

For passwords or sensitive text, use the getpass module instead of input(). It hides what the user types. The function is getpass.getpass("Password: "), and you import it with import getpass first. Labels can differ slightly by Python version, but the module name has stayed the same.

After collecting input, echo it back before you act on it. A line like print("You chose " + choice + ".") lets the user spot a typo. This is especially useful before a step that is hard to undo, such as deleting a file or sending a message.

For numbers, format them so they read well. round(value, 2) trims a decimal to two places. An f-string such as f"Total: {total:.2f}" prints the same result with a fixed two decimals. Both are standard and safe to use.

If you collect several answers, print a short summary at the end. Three or four lines are enough. The user sees what the program understood and can correct it before anything else happens.

Keep Prompts in One Place as Your Script Grows

When a script has five or more prompts, the questions start to scatter. Put the prompt text in variables at the top of the file, or in a small dictionary, so you can edit wording in one spot. This also makes it easier to translate later if you ever need to.

Name your variables after the answer, not the question. user_name is clearer than q1. Clear names make the rest of the code easier to follow, which matters when you return to it after a week away.

Test each prompt with three kinds of input: a normal answer, an empty line, and something wrong. See how the program behaves. Fix the message if it confuses you; it will confuse someone else too.

What if the user just presses Enter?

input() returns an empty string, "". If you convert that with int(), you get a ValueError, which your loop can catch. If you keep it as text, check for the empty string and ask again with a gentle message.

Can I give the user a default answer?

Yes. Show the default in the prompt, then check for an empty answer. If the answer is empty, use your default value. This saves typing for the common case and still lets the user override it.

Why does my prompt print on the same line as the answer?

That is how input() works: the prompt and the typing share a line. If you want the answer on a new line, add \n at the end of the prompt string, or print the question first and call input() with no text.

How do I ask for a list of items?

Ask for one line with items separated by commas, then use split(",") to break it into a list. Strip extra spaces from each item with .strip() so the list stays clean. Print the list back so the user can confirm it.