1.5 print(), input(), and Console Tracing
Key Takeaways
- `print()` writes text to standard output, converts each argument to text, and returns `None`.
- `print()` uses `sep=' '` between multiple arguments and `end='\n'` after the last argument unless told otherwise.
- `input()` optionally displays a prompt and always returns a string with the submitted line, excluding the final newline.
- Numeric input must be converted explicitly before arithmetic, commonly with `int()` or `float()`.
- Console tracing means tracking variable values and exact output line by line, including spaces and newline behavior.
Output with print()
The built-in print() function sends text to standard output. It can receive zero, one, or many arguments. Each argument is converted to text, the arguments are joined by the sep value, and the end value is written after the final argument.
By default, sep is a single space and end is a newline.
print('Python', 3)
print('ready')
The output is:
Python 3
ready
The first call prints two arguments separated by one space, then a newline. The second call starts on the next line.
| Parameter | Default | Effect |
|---|---|---|
sep | ' ' | Text placed between arguments |
end | '\n' | Text placed after the last argument |
print('A', 'B', sep='-', end='!')
print('C')
This prints A-B!C on one line because the first call ends with ! instead of a newline. PCEP output questions often hide their answer in sep and end, not in complicated logic.
Remember that print() returns None. It is useful for displaying information, not for handing a calculated value back to another expression.
Input with input()
The built-in input() function reads a line of text from standard input. If you pass a prompt, Python displays it first. The returned value is always a string.
name = input('Name: ')
print('Hello,', name)
If the user types Mina, the variable name receives the string 'Mina'. The newline from pressing Enter is not included in the returned string.
For numeric work, convert explicitly:
hours = int(input('Hours: '))
rate = float(input('Rate: '))
print(hours * rate)
Without conversion, input() gives text. If a user enters 4, the value is '4', not 4. Multiplying '4' * 3 produces string repetition, while adding '4' + 3 raises TypeError.
Console tracing
Console tracing is the skill of predicting exact output and variable values without running the code. PCEP uses short snippets because they reveal whether you understand execution order.
Trace this code:
x = 2
print(x)
x = x + 3
print('x =', x)
| Step | Statement | Important state | Output |
|---|---|---|---|
| 1 | x = 2 | x is 2 | none |
| 2 | print(x) | x still 2 | 2 |
| 3 | x = x + 3 | x becomes 5 | none |
| 4 | print('x =', x) | x is 5 | x = 5 |
When tracing, write down output exactly: spaces, punctuation, and line breaks count. Do not assume a space appears after a prompt unless the prompt string includes one. Do not assume print() adds a newline if end was changed.
Exam approach
For each I/O snippet, ask:
- What values are entered, and are they strings or converted numbers?
- What is the current value of each variable before every
print()call? - What are the active
sepandendvalues? - Does the question ask for displayed output or returned value?
That last distinction matters: displaying 12 and returning 12 are different behaviors in Python.
What is printed by print('A', 'B', sep='-', end='!')?
If the user types 12 at an input() prompt, what type is returned before conversion?
What does print() return after displaying its output?