Understanding the input() Function
Use input('prompt string') to display a message before pausing for user input
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.
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.
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, 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?
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.
Favorite color: blue
blueWithout 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.
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.
What is your name?
Alice
AliceHere, 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 arrangement | Cursor position | User experience |
|---|---|---|
| Prompt without \n | Same line as the prompt | Compact, but the prompt and response share one line |
| Prompt ending with \n | Fresh line below the prompt | Creates 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.
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
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
- Pass a string argument to input() to display a prompt before the program waits for user input.
- Without a prompt, the user has no context about what to type.
- By default, the user's cursor appears on the same line as the prompt.
- Use \n to move the cursor to a new line below the prompt.
- 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.