4.12 Output
print() in depth — the sep and end parameters, printing escape characters, and using f-strings for formatted output.
print()
The standard way to write text to the console. Accepts any number of arguments, converts each to a string, and prints them separated by spaces by default.
>>> print("Hello", "World")
Hello World
sep
What Is It?
The sep parameter controls what string is inserted between multiple arguments (default is a single space).
Why Is It Used?
Quick formatting of joined output without building a string manually first.
>>> print("a", "b", "c", sep="-")
a-b-c
end
What Is It?
The end parameter controls what’s printed after all the arguments (default is a newline \n).
Why Is It Used?
To print without starting a new line afterward, e.g. building output incrementally on the same line.
>>> print("x", end="")
>>> print("y")
xy
Escape Characters
A quick preview here — the full reference is 4.13 Escape Characters.
>>> print("Line1\nLine2")
Line1
Line2
Formatted Output
f-strings (Python 3.6+) are the modern, preferred way to embed variables directly into printed text.
>>> name, score = "Alice", 95.5
>>> print(f"{name} scored {score}%")
Alice scored 95.5%
Quick Interview Answer
“
print()converts every argument to a string, joins them withsep(default a single space), and finishes withend(default a newline). Settingend=\"\"is the standard way to print multiple things on the same line. f-strings (f\"{name} scored {score}%\") are the modern way to embed variables directly into output, rather than manual string concatenation.”
Common Mistakes
- Forgetting that
print()adds a trailing newline by default — leads to unexpected extra blank lines when building output piece by piece without settingend="". - Using old-style
%formatting or manual+concatenation for new code instead of f-strings, making output harder to read and edit.
Add More Questions to This Guide
Know a question that should be here? Share it and help the community!
Open Google Form