Using print() to Display Output
Although print() displays a value, input() brings data into the program.
From Fixed Data to User Data
A program can use values written directly inside its code, but real programs also need information from the person running them. A banking app may ask for an account number, a game may ask for a name, and a calculator may wait for numbers. Python provides the input() function for this kind of interaction. Although print() displays a value, input() brings data into the program.
The central behavior is simple: input() pauses the program, waits for keyboard input, and then returns what the user entered as a string.
The Pause at input()
What do you think happens?
What happens when this program reaches input() and the user types Alice before pressing Enter?
Reveal answer
The program pauses, waits for the entry, and then continues.
The program reaches input() and stops at that point while the cursor waits for keyboard input. After the user types Alice and presses Enter, input() returns the string Alice, and execution moves to the next statement.
The important point is that later statements do not run while input() is waiting. If a program contains a print() call after input(), that print() call happens only after the user has entered data and pressed Enter.
Storing the Returned String
The usual pattern is to assign the result of input() to a variable. In name = input(), input() reads from the keyboard and returns the entered data. The assignment operator stores that returned string in the variable name. The variable can then be used by later statements in the program.
Tracing a Name Through the Program
Follow the program when the user types Alice and presses Enter.
- 1Reach the first statement
The program reaches
input()and pauses while it waits for keyboard input. - 2Submit the entry
The user types Alice and presses Enter.
input()returns the string Alice. - 3Assign the value
The assignment stores the returned string in the variable name.
- 4Run the next statement
Execution moves to
print(name), which displays the value stored in name.
The displayed value is Alice. Nothing from the print() statement is displayed until after the input has been provided.
Strings Even When Input Looks Numeric
input() always returns a string, regardless of what the user types. If the user types 25, the returned value is the string '25', not the integer 25. This distinction matters because strings and integers behave differently in a program.
Order of Multiple Inputs
Each input() call runs when execution reaches that call. If one input() statement appears before another, the first one pauses the program first. After the first entry is submitted, execution continues until it reaches the next input() call, where it pauses again.
In this example, the program waits at the first input() call. It cannot reach the second input() call until the first entry has been submitted. It then waits again at the second call. Only after both values have been collected can the two print() statements run.
Mistakes with Collected Data
Common mistake Expecting input() to continue immediately
What it looks likeThe program appears to stop at input().Why it failsPausing is the intended behavior.
input()waits for keyboard input before execution continues.Do this insteadType a value and press Enter, then observe the next statement run.
Common mistake Using input() without storing its result
What it looks likeinput()Why it failsThe entered data is returned as a string, but without assignment there is no named variable available for later use.
Do this insteadAssign the result to a variable, such as name = input().
Common mistake Treating numeric-looking input as an integer automatically
What it looks likeage = input()Why it failsEven when the user types 25,
input()returns the string '25'.Do this insteadConvert the returned string with int() or float() before performing arithmetic.
Common mistake Expecting print() to run before input() is answered
What it looks likeA print() statement appears after input(), but nothing is displayed yet.
Why it failsStatements after the waiting
input()call do not run until the user submits an entry.Do this insteadTrace the statements in order and remember that execution resumes only after input is provided.
Testing Interactive Programs
After writing a program that uses input(), run it and type different entries. Check that the program pauses where the input() call occurs, resumes after you submit the entry, and uses the exact text you entered in later output. Testing different entries helps you verify both the execution flow and the data passed through the variable.
Write a short program that collects a user's name with input(), stores it in a variable, and then displays that variable with print(). Before running it, identify the exact line where the program will pause.
Hints
- Assign
input()to a variable. - The
print()statement should use that variable. - The pause occurs when execution reaches
input().
Key Takeaways
- 1
input()pauses program execution and waits for keyboard input. - 2After the user submits an entry,
input()returns that entry as a string. - 3Assign the returned value to a variable so later statements can use it.
- 4Use
int()orfloat()to convert string input before performing arithmetic. - 5
print()displays stored data, whileinput()collects data and controls when execution resumes.
At a glance
input()creates a pause in the program while it waits for keyboard input.- Every value returned by
input()is a string, even when the entry looks like a number. - Assign
input()to a variable to preserve and reuse the entered data. - Statements run in order, so later
input()andprint()calls wait until earlier input has been submitted. - Convert string input with
int()orfloat()before performing arithmetic.