Output Variables & Printing Techniques0%
Output Variables & Printing Techniques
Beginner8 min read•Updated: 2026-09-03
Output Variables & Printing Techniques
The print() function is the fundamental way to display program output to the console.
Key Concepts & Detailed Explanation
print() accepts several optional keyword arguments:
- sep: String inserted between values, default is space ' '.
- end: String appended after the last value, default is newline '\n'.
- file: Specifies where to write (defaults to sys.stdout).
Code Examples & Output
Python
# Using sep and end
print("2026", "09", "03", sep="-")
print("Loading data", end=" -> ")
print("Complete!")
# Printing multiple expressions
item = "Laptop"
price = 45000
print("Item:", item, "| Price: ₹", price)
Expected Output:
Output
2026-09-03
Loading data -> Complete!
Item: Laptop | Price: ₹ 45000
Best Practices & Common Pitfalls
Use 'end=""' when building progress bars or continuous console prompts.
Practice Quiz
1. What is the output of print('A', 'B', sep='*')?
- A) A B
- B) A*B
- C) AB*
- D) *AB
Answer: B
Explanation: sep='*' places an asterisk between arguments.
2. How do you prevent print() from starting a new line?
- A) print(..., newline=False)
- B) print(..., end='')
- C) print(..., stop=True)
- D) print(..., sep='')
Answer: B
Explanation: Setting end='' overrides the default '\n'.
Practice Challenge
Print the numbers 1, 2, 3, 4, 5 on the same line separated by ' -> ' with no trailing newline.
Next Lesson
Multiple Assignments
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Creating a Variable | Multiple Assignments |
Practice Quiz
Test your understanding of this lesson with 1 questions. Each question has one correct answer.