Understanding the input() Function
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.
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
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 |
The newline character controls where the user's input begins.
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
Common mistake Calling input() without telling the user what to enter
What it looks likeresponse = input()Why it failsThe program pauses, but the user sees no prompt explaining what information is expected.
Do this insteadPass a clear string argument, such as input('Enter a response: ').
Common mistake Assuming the cursor automatically moves below the prompt
What it looks likename = input('What is your name?')Why it failsBy default, the cursor appears on the same line as the prompt.
Do this insteadAdd \n to the prompt when the response should begin on the next line.
Common mistake Adding a newline without checking the visual result
What it looks likename = input('What is your name?\n')Why it failsThe intended layout should be verified by running the program and observing where the cursor appears.
Do this insteadTest 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.
- 1Choose a variable
Use a variable to hold the string returned after the user enters a response.
- 2Add the prompt
Pass a prompt string to
input()so the user knows what information to provide. - 3Move the cursor
Put \n at the end of the prompt so the cursor moves to the next line.
- 4Display the response
Pass the variable to
print()to output the entered string.
book = input('What is your favorite book?\n')
print(book)Key Takeaways
- 1Pass a string argument to
input()to display a prompt before the program waits for user input. - 2Without a prompt, the user has no context about what to type.
- 3By default, the user's cursor appears on the same line as the prompt.
- 4Use \n to move the cursor to a new line below the prompt.
- 5Run and inspect prompts visually so the final layout is clear and usable.
At a glance
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.