Storing and Displaying User Data with Variables
A prompt gives the user that missing context by explaining what the program expects.
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
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.
What is your name? Alice
AliceIn 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
Following the name example
Trace what happens when the user enters Alice for the prompt What is your name?
- 1Display
The string What is your name? is displayed before the input pause.
- 2Pause
The program waits for the user to type something.
- 3Enter text
The user types Alice and presses Enter.
- 4Store
The variable name holds the string Alice.
- 5Display 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.
| Prompt form | Cursor position | Visual effect |
|---|---|---|
| input('Name?') | Same line as the prompt | The user's text begins beside the prompt |
| input('Name?\n') | Next line below the prompt | The user's text is visually separated from the prompt |
The newline character changes the position of the user's cursor.
What is your name?
Alice
AlicePrompt Mistakes to Avoid
Common mistake Calling input() without a prompt
What it looks likename = input()Why it failsThe program pauses, but the user receives no explanation of what to type.
Do this insteadProvide a message such as input('What is your name?').Common mistake Ignoring where the cursor appears
What it looks likeA long prompt leaves the user's text directly beside the message.
Why it failsThe screen can look cluttered or confusing.
Do this insteadConsider ending the prompt with \n when a separate input line is clearer.
Common mistake Not testing the prompt visually
Why it failsThe written prompt may seem clear, but its actual screen layout may place the cursor somewhere unexpected.
Do this insteadRun 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
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()afterinput()returns.
What do you think happens?
Before running the program, predict where the user's cursor appears for input('What is your name?\n').
Reveal 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
- 1Use
input('prompt string')to display a message before the program waits for user input. - 2The user's entered text is returned by
input()and can be stored in a variable. - 3By default, the cursor appears on the same line as the prompt.
- 4Add \n to the prompt when the user's input should begin on the next line.
- 5Test 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.