Concepts / Storing and Displaying User Data with Variables

Storing and Displaying User Data with Variables

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

  • Programming

A Blank Cursor Gives No Clue

When a program calls input(), it pauses and waits for the user to type something. If the program does not display a message first, the user sees a blank cursor without knowing what information to provide. A prompt gives the user that missing context by explaining what the program expects.

The Input Interaction

displaysthen waitsuser typesreturned asinput()program callPromptmessage displayedPausedwaiting for inputUser texttext enteredVariable valuestored string
What happens first, while the program is paused, and after the user enters text?

The input() function accepts an optional string argument. That string is displayed before the program pauses. After the user types something and presses Enter, the entered text becomes the value returned by input(), so the program can store it in a variable.

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. The later print(name) displays Alice on the next line.

From Prompt to Stored Value

guidesbecomesstored inPrompt messageWhat is your name?Aliceuser-entered textinput() resultreturned stringnameAlice
How does the text entered by the user move from the prompt into a value the program can store?

Following the name example

Trace what happens when the user enters Alice for the prompt What is your name?

Display: The string What is your name? is displayed before the input pause.

Pause: The program waits for the user to type something.

Enter text: The user types Alice and presses Enter.

Store: The variable name holds the string Alice.

Display again: print(name) outputs Alice on the next line.

The prompt guides the user, input() returns the entered text, and name stores that string.

Moving the Cursor with \n

By default, when a prompt is passed to input(), the user's cursor appears on the same line as the prompt text. This can make the screen look cluttered. The newline character, written as \n, tells Python to move to the next line.

default position\n moves downName?promptName?promptCursorsame lineCursornext line
How does adding \n change where the prompt ends and where the user's cursor appears?
Prompt formCursor positionVisual effect
input('Name?')Same line as the promptThe user's text begins beside the prompt
input('Name?\n')Next line below the promptThe user's text is visually separated from the prompt
python
Output
What is your name?
Alice
Alice

Prompt Mistakes to Avoid

  • Calling input() without a prompt

    The program pauses, but the user receives no explanation of what to type.

    Fix: Provide a message such as input('What is your name?').

  • Ignoring where the cursor appears

    The screen can look cluttered or confusing.

    Fix: Consider ending the prompt with \n when a separate input line is clearer.

  • Not testing the prompt visually

    The written prompt may seem clear, but its actual screen layout may place the cursor somewhere unexpected.

    Fix: Run the program and observe exactly where the prompt ends and where the cursor appears.

Write prompts that clearly identify the information expected from the user. Then run the program and inspect the screen layout. A prompt is successful only when the message is understandable and the cursor appears where the user expects to type.

Practice the Interaction

EASY

Create a prompt that asks the user for their name and places the cursor on a new line below the question. Store the user's response in a variable, then display that stored value.

Hints
  • Pass the prompt message as the string argument to input().
  • Put \n at the end of the prompt string to move the cursor down.
  • Use the variable in print() after input() returns.

What do you think happens?

Before running the program, predict where the user's cursor appears for input('What is your name?\n').

  • Immediately after the question on the same line
  • On the next line below the question
  • Before the question
Reveal answer

Answer: On the next line below the question

The newline character tells Python to make a line break at the end of the prompt, so the user's cursor appears on the following line.

The Prompt-to-Variable Pattern

  1. Use input('prompt string') to display a message before the program waits for user input.
  2. The user's entered text is returned by input() and can be stored in a variable.
  3. By default, the cursor appears on the same line as the prompt.
  4. Add \n to the prompt when the user's input should begin on the next line.
  5. Test prompts visually to check both their wording and the cursor position.

Key Takeaways

  • A prompt tells the user what information the program expects before input() pauses.
  • The string argument to input() is displayed before the program waits for typing.
  • Text entered by the user is returned by input() and can be stored in a variable.
  • The newline character \n places the cursor on the next line below the prompt.
  • Clear formatting and visual testing make prompts easier to understand and use.