Computer Science Grade 10 20 min

Print Statements

Print Statements

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Format strings dynamically for clean and readable output using f-strings. Control line endings and separators within print statements to create custom layouts like progress bars. Differentiate between standard output (stdout) and standard error (stderr) for effective program logging and error reporting. Implement the `__str__` method in a class to define a custom, human-readable representation of an object. Use indented print statements to trace the execution flow and call stack of a recursive function for debugging. Utilize escape sequences like `\n` and `\t` to precisely control whitespace and formatting in console output. Ever wondered how developers debug complex code without a fancy visual tool, or how command-line tools show those cool updating prog...
2

Key Concepts & Vocabulary

TermDefinitionExample Formatted String Literal (f-string)A string prefixed with 'f' that allows you to embed expressions, like variable values, directly inside curly braces `{}` within the string.name = 'Alex'; age = 15; print(f'Student: {name}, Age: {age}') Standard Output (stdout)The default data stream where a program writes its normal output. When you use a standard `print()`, it writes to stdout.print('This message goes to standard output.') Standard Error (stderr)A separate data stream used for a program's error messages or diagnostics. This allows errors to be seen even if normal output is redirected to a file.import sys; print('Error: File not found.', file=sys.stderr) The `end` parameterAn optional keyword argument in the `pr...
3

Core Syntax & Patterns

f-string Formatting Syntax f'text {expression:format_specifier} text' Use this for embedding variables and expressions directly into strings. The optional format specifier controls padding, alignment, and number formatting (e.g., `{price:.2f}` for two decimal places). Print Function Signature print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False) This shows the key optional arguments for the print function. Use `sep` to change the separator between items, `end` to change the character at the end of the line, and `file` to redirect output (e.g., to `sys.stderr`).

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
Analyze the precise console output of this sequence of print calls: `print('A', 'B', sep='*', end='-') print('C', 'D', sep='|', end='!')`
A.A*B-C|D!
B.A*B\n-C|D!
C.A*B-C|D!
D.A-B*C!D|
Challenging
You are debugging a recursive merge sort algorithm. You want to trace the function calls, showing the list segment being sorted at each step. Which pair of print statements, placed at the beginning and end of the function, would be most effective for visualization?
A.Start: `print("Sorting...")` | End: `print("Done.")`
B.Start: `print(f"{' ' * depth}Sorting: {sub_list}")` | End: `print(f"{' ' * depth}Merged: {result_list}")`
C.Start: `print(sub_list, file=sys.stderr)` | End: `print(result_list, file=sys.stdout)`
D.Start: `print(f"Call with {sub_list}", end='')` | End: `print(f" returns {result_list}")`
Challenging
You are implementing a console-based progress bar that needs to update on a single line. Which combination of a `print` parameter and an escape sequence is most effective for moving the cursor back to the beginning of the line to overwrite the previous output?
A.`end='\n'` and `\b` (backspace)
B.`end=''` and `\r` (carriage return)
C.`sep=''` and `\t` (tab)
D.`end=' '` and `\a` (alert bell)

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Advanced Topics

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.