Concepts / Understanding the input() Function

Understanding the input() Function

Use input('prompt string') to display a message before pausing for user input

  • Programming

From Pause to Response

When a program calls input(), it pauses and waits for the user to type something. A blank waiting cursor does not explain what the user should enter, so the program needs to give the user context. A prompt is the message shown before the input pause.

displays promptguides responseenters inputProgramScreenprompt textUsertyped response
What happens first, and how does control flow move from displaying the prompt to waiting for the user's response?

The important sequence is: Python displays the prompt, input() pauses, the user types a response, and the program can continue after the response is entered.

Prompt Strings

The input() function accepts an optional string argument. Put the prompt message inside the parentheses as a string. Python displays that string to the user before waiting for input.

python
Output
What is your name?Alice
Alice

In this example, What is your name? is the prompt. The program displays it and waits. If the user types Alice and presses Enter, the variable name holds the string Alice, and print(name) outputs Alice on the next line.

A prompt is a message that appears before the input pause and guides the user about what kind of information to provide.

Tracing Cursor Placement

What do you think happens?

Where will the user begin typing after this prompt is displayed?

  • On the same line immediately after the prompt
  • On a new line below the prompt
Reveal answer

Answer: On the same line immediately after the prompt

By default, when a prompt is passed to input(), the user's cursor appears on the same line as the prompt text.

python
Output
Favorite color: blue
blue

Without a newline character, the user's typing begins on the same line as the prompt. This can be perfectly readable for a short prompt, but longer prompts or crowded screen layouts may make the interaction harder to follow.

typing begins after prompt\n places cursor belowFavorite color:cursor follows promptFavorite color:cursor moves belowbluesame linebluenew line
Where does the user's cursor appear when the prompt contains \n compared with when it does not?

The Newline Character

The newline character is written as \n. It is a special sequence that tells Python to move to the next line. When \n is included at the end of an input() prompt, the user's input cursor appears on a fresh line below the prompt.

python
Output
What is your name?
Alice
Alice

Here, the prompt ends with \n, so the cursor moves below What is your name? before the user types. The response is visually separated from the prompt, and the later print(name) output appears after the response.

Readable User Prompts

A prompt should make the expected response understandable. Clear, well-formatted prompts improve usability and make programs easier to understand. The choice between same-line input and input on a new line depends on which arrangement is easiest for the user to read.

Prompt arrangementCursor positionUser experience
Prompt without \nSame line as the promptCompact, but the prompt and response share one line
Prompt ending with \nFresh line below the promptCreates visual separation between the instruction and response

Always test prompts visually. Run the program and observe the screen layout so you can confirm that the cursor appears where you expect it.

python

Common Prompt Mistakes

  • Calling input() without telling the user what to enter

    The program pauses, but the user sees no prompt explaining what information is expected.

    Fix: Pass a clear string argument, such as input('Enter a response: ').

  • Assuming the cursor automatically moves below the prompt

    By default, the cursor appears on the same line as the prompt.

    Fix: Add \n to the prompt when the response should begin on the next line.

  • Adding a newline without checking the visual result

    The intended layout should be verified by running the program and observing where the cursor appears.

    Fix: Test the prompt visually and adjust the formatting to make the interaction clear.

Prompt Practice

EASY

Write an input() statement that asks the user for a favorite book and places the user's cursor on a new line below the prompt. Then add a print() statement that displays the response.

Hints
  • Put the prompt text inside the parentheses as a string.
  • Place \n at the end of the prompt to move the cursor to the next line.
  • Store the response in a variable so print() can display it.

A New-Line Book Prompt

Create a prompt for a favorite book with the response entered below the prompt.

Choose a variable: Use a variable to hold the string returned after the user enters a response.

Add the prompt: Pass a prompt string to input() so the user knows what information to provide.

Move the cursor: Put \n at the end of the prompt so the cursor moves to the next line.

Display the response: Pass the variable to print() to output the entered string.

book = input('What is your favorite book?\n') print(book)

Key Takeaways

  1. Pass a string argument to input() to display a prompt before the program waits for user input.
  2. Without a prompt, the user has no context about what to type.
  3. By default, the user's cursor appears on the same line as the prompt.
  4. Use \n to move the cursor to a new line below the prompt.
  5. Run and inspect prompts visually so the final layout is clear and usable.

Key Takeaways

  • input() can accept a prompt string that tells the user what to enter.
  • Calling input() pauses the program while it waits for the user's response.
  • The default cursor position is on the same line as the prompt.
  • Adding \n moves the cursor to the next line and can create clearer visual separation.
  • Prompt formatting should be tested visually for readability and usability.