← Back to Hub
Module 1 Week 3 • print() Function & Outputs Formatting

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 a TypeError if 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')?

Explanation: The + operator joins strings exactly as they are without inserting spaces.

Question 2: Which code snippet will raise a TypeError?

Explanation: You cannot concatenate a string and a raw integer using + without casting first.

Question 3: What spacing does the comma separator in print() add between arguments?

Explanation: Commas automatically insert a single space character between printed arguments.

Question 4: Which escape code wraps the subsequent text onto a new line?

Explanation: \n is the newline escape character in Python.

Question 5: What does the escape code '\t' do?

Explanation: \t inserts a tab spacer for horizontal alignment.

Question 6: What is the output of: print('A', 'B', sep='*')?

Explanation: The sep parameter specifies the character to insert between comma-separated items.

Question 7: How can you print a text string that contains double quotes inside double quotes?

Explanation: Using \" escapes the quote so the interpreter does not treat it as the end of the string block.

Question 8: What is the output of print('A' + ' ' + 'B')?

Explanation: Concatenating 'A', a space ' ', and 'B' yields the spaced string 'A B'.

Question 9: What happens when you run print() with no arguments inside the parentheses?

Explanation: Calling print() without arguments prints an empty newline character.

Question 10: Which symbol escapes special formatting characters inside strings?

Explanation: The backslash \ is the escape prefix character in Python strings.

🖥️ Output Prediction Challenges

Read each code snippet carefully, analyze its execution flow, and predict the exact string result written to the console.

Challenge 1 of 5

Predict the output of the code snippet below. Type the exact output in the console.

print("A" + "B")
Console ready. Type your prediction below...
Challenge 2 of 5

Predict the output of the code snippet below. Type the exact output in the console.

print("A", "B")
Console ready. Type your prediction below...
Challenge 3 of 5

Predict the output of the code snippet below. Type the exact output in the console.

print("Line 1\nLine 2")
Console ready. Type your prediction below...
Challenge 4 of 5

Predict the output of the code snippet below. Type the exact output in the console.

print("A", "B", sep="-")
Console ready. Type your prediction below...
Challenge 5 of 5

Predict the output of the code snippet below. Type the exact output in the console.

print("A\tB")
Console ready. Type your prediction below...

🎮 Interactive Code Playground

Type your own Python code below and click "Run Code" to execute it instantly in your browser!

📝 Python Code Editor
Python WebAssembly runtime is unloaded. Click 'Run Code' to load.
🖥️ Output Console
Console ready.
💬 Teacher Consultation

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

Lesson Complete?

📌 Check the box above to mark this weekend's lesson complete. Your dashboard status ring will update automatically.