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 arguments and `end='\n'` after the last argument unless overridden.
- `input()` optionally shows a prompt and always returns a `str`, with the trailing newline stripped.
- Numeric input must be converted explicitly with `int()` or `float()` before arithmetic.
- Console tracing means predicting exact output and variable values line by line, including spaces and newlines.
Output with print()
The built-in print() function sends text to standard output. It accepts zero, one, or many positional arguments. Each is converted to text (via str()), the arguments are joined by the sep value, and the end value is written after the final argument.
The defaults are sep=' ' (one space) and end='\n' (a newline).
print('Python', 3)
print('ready')
Output:
Python 3
ready
The first call prints two arguments separated by a single space, then a newline; the second starts on the next line. print() with no arguments simply prints a blank line.
| Parameter | Default | Effect |
|---|---|---|
sep | ' ' | Text placed between arguments |
end | '\n' | Text placed after the last argument |
file | sys.stdout | Destination stream |
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, so the next call continues on the same line. PCEP output items often hide their answer in sep and end rather than in complex logic. Also remember that print() returns None: it is for display, not for handing a value back, so x = print('hi') binds None to x.
Input with input()
The built-in input() function reads one line from standard input. If you pass a prompt string, Python displays it first (with no automatic trailing space). The returned value is always a str, and the newline from pressing Enter is stripped.
name = input('Name: ')
print('Hello,', name)
If the user types Mina, then name is 'Mina'. For numeric work you must 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. Then '4' * 3 performs string repetition ('444'), and '4' + 3 raises TypeError. This string-versus-number trap is one of the most tested ideas in Section 4 (I/O) and appears in fundamentals too.
Console tracing
Console tracing is predicting exact output and variable values without running the code. PCEP uses short snippets precisely because they expose whether you understand execution order. Trace this:
x = 2
print(x)
x = x + 3
print('x =', x)
| Step | Statement | State after | Output |
|---|---|---|---|
| 1 | x = 2 | x is 2 | (none) |
| 2 | print(x) | x still 2 | 2 |
| 3 | x = x + 3 | x is 5 | (none) |
| 4 | print('x =', x) | x is 5 | x = 5 |
Write output exactly, counting spaces, punctuation, and line breaks. Two reliable rules: a space appears after a prompt only if the prompt string includes one, and print() adds a newline only if end was not changed. The assignment x = x + 3 reads the old value (2) on the right, computes 5, then rebinds x; tracing the right side before the left avoids errors.
Exam approach
For each I/O snippet, ask in order:
- What was entered, and is each value still a string or already converted to a number?
- What is the current value of every variable just before each
print()call? - What are the active
sepandendvalues for this particular call? - Does the question want the displayed output or a returned value?
That last distinction is decisive: displaying 12 writes characters to the console, while returning 12 hands a value to surrounding code, and print() only ever does the former and returns None.
Multiple arguments, sep, and end interactions
Many exam items combine several print() calls and ask for the exact concatenated output. The rule is mechanical: within one call, arguments are converted to text and joined by sep; then end is appended; then the next call begins wherever the cursor now sits. Consider three calls: print('x', end=''), print('y', end=''), print('z'). The result is xyz followed by a newline, all on one line, because the first two suppress the newline. If instead all three used defaults, the output would be three separate lines.
Because sep and end accept any string, including the empty string '', the safest tracing method is to literally write each emitted character in order, never assuming an invisible space or newline.
input() prompt behavior and EOF
The prompt argument to input() is itself printed with no trailing space, so input('Age:') shows Age: immediately before the cursor; writing input('Age: ') adds the space deliberately. The prompt is optional, so bare input() simply waits for a line. The exam treats input as a clean line of text; in practice an end-of-input condition raises EOFError, but PCEP focuses on the normal case where a value is supplied. Remember that everything typed before Enter becomes the string, including leading and trailing spaces, which are not stripped automatically; only the newline is removed.
Putting it together
A typical Section 4 question reads two numbers and prints a computed result. The correct pattern wraps each input() in int() or float(), performs arithmetic, and prints with deliberate sep/end. Trace such snippets by recording entered strings, the converted numeric values, each variable's state before every print(), and the active sep/end, then write the output character by character to match an answer option exactly.
What is printed by print('A', 'B', sep='-', end='!')?
If the user types 12 at an input() prompt, what type does input() return before any conversion?
What does print() return after it finishes displaying its output?