Displaying Outputs
Master output methods: printing multiple items with commas, string concatenation (+), default spaces, and basic escape codes.
1. The print() Function
We use the built-in print() function to send text messages and outputs to the screen.
2. Commas vs. Plus (+) in print()
- Comma separation (
,): Combines multiple items of any type, automatically adding a space between them. E.g.,print("Age:", 22)works perfectly! - Concatenation (
+): Joins strings together directly. It does NOT add spaces automatically and will raise aTypeErrorif you try to join a string and a number.
3. Escape Sequences
Escape characters let you format the output layout inside a string. They begin with a backslash \:
\n: Moves the text following it onto a new line.\t: Inserts a horizontal tab space (equal to 4 spaces) for alignment.
💻 Python Code Examples
Example 1: Printing Text Strings
print("Hello, Python Course!")
Description: Outputs a standard text string to the terminal console.
Example 2: Comma Separated Outputs
print("Python", "is", "fun")
Description: Uses commas to print multiple strings, automatically inserting spaces.
Example 3: Concatenating Text with +
print("Python" + "is" + "fun") # Output: Pythonisfun
Description: Joins text strings together directly with no spaces added.
Example 4: Mixing Types with Commas
print("Score:", 100)
Description: Safely combines text and a number using a comma without throwing errors.
Example 5: Formatting with Newlines (\n)
print("Line 1\nLine 2")
Description: Uses \n to break the output string into two lines.
Example 6: Formatting with Tabs (\t)
print("Item:\tBook\nPrice:\t$15")
Description: Uses \t to insert tabs and align text attributes.
🧠 Active Retrieval Quizzes
Answer the following 10 multiple-choice questions to test your storage strength.
Question 1: What is the output of print('Hello' + 'World')?
Question 2: Which code snippet will raise a TypeError?
Question 3: What spacing does the comma separator in print() add between arguments?
Question 4: Which escape code wraps the subsequent text onto a new line?
Question 5: What does the escape code '\t' do?
Question 6: What is the output of: print('A', 'B', sep='*')?
Question 7: How can you print a text string that contains double quotes inside double quotes?
Question 8: What is the output of print('A' + ' ' + 'B')?
Question 9: What happens when you run print() with no arguments inside the parentheses?
Question 10: Which symbol escapes special formatting characters inside strings?
🖥️ Output Prediction Challenges
Read each code snippet carefully, analyze its execution flow, and predict the exact string result written to the console.
Predict the output of the code snippet below. Type the exact output in the console.
print("A" + "B")
Predict the output of the code snippet below. Type the exact output in the console.
print("A", "B")
Predict the output of the code snippet below. Type the exact output in the console.
print("Line 1\nLine 2")
Predict the output of the code snippet below. Type the exact output in the console.
print("A", "B", sep="-")
Predict the output of the code snippet below. Type the exact output in the console.
print("A\tB")
🎮 Interactive Code Playground
Type your own Python code below and click "Run Code" to execute it instantly in your browser!
Console ready.
Stuck on any concept, or want to explore an edge case? Don't hesitate to ask follow-up questions to your instructor! Active questioning helps build storage strength.
📋 Progress Status
📌 Check the box above to mark this weekend's lesson complete. Your dashboard status ring will update automatically.