print()
The everyday output tool — customize the separator, the ending, or the file. Not for formatted logging.
Common call
print("hello", name)
Returns
None; text goes to stdout with a trailing newline
Replaces
the Python 2 `print` statement
Watch out
sep is BETWEEN objects (default space); end is AFTER the last (default newline); pass end="" to suppress the newline
print(*objects, sepsep — String inserted between objects. Default is a single space.type: str · default: " "=' ', endend — String appended after the last object. Default is a newline. Pass "" to suppress the trailing newline.type: str · default: "\n"='\n', filefile — Destination file-like object. Use sys.stderr for error output.type: file · default: sys.stdout=sys.stdout, flushflush — If True, flush the file buffer after writing. Useful for real-time output in scripts.type: bool · default: False=False)
→ None
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| *objects | Any | no (()) | Zero or more values to print. Each is converted with str(). |
| sep | str | no (" ") | String inserted between objects. Default is a single space. |
| end | str | no ("\n") | String appended after the last object. Default is a newline. Pass "" to suppress the trailing newline. |
| file | file | no (sys.stdout) | Destination file-like object. Use sys.stderr for error output. |
| flush | bool | no (False) | If True, flush the file buffer after writing. Useful for real-time output in scripts. |
Return value
None — Returns None. The output is written to the file (default stdout). Objects are converted via str(); joined with sep between them; end is appended after the last one.
Common patterns
Print without newline
Progress bars, prompts, and inline output.
print("Loading...", end="")
Change the separator
Comma, tab, or a custom joiner between multiple values.
print("a", "b", "c", sep=" | ") # "a | b | c"
Print to stderr
Errors and diagnostics should not pollute stdout.
import sys print("error:", msg, file=sys.stderr)
Force flush for real-time output
Buffered stdout may not appear immediately — flush ensures visibility.
print("tick", end="", flush=True)
Prefer logging for anything beyond scripts
For applications, `logging` is the right tool — print is for one-off scripts and prototypes.
import logging logging.info("something happened: %s", detail)
Examples
1. Basic
print("hello", "world")
Returns
"hello world\n" # to stdout2. Custom sep
print("a", "b", "c", sep="-")
Returns
"a-b-c\n"3. No newline
print("hi", end="")
Returns
"hi" # no trailing \n4. No sep
print("a", "b", sep="")
Returns
"ab\n"5. Multiple types
print(1, "two", 3.0, None)
Returns
"1 two 3.0 None\n"6. To stderr
print("err", file=sys.stderr)
Returns
"err\n" # goes to stderr7. Empty print
print()
Returns
"\n" # just a blank linePitfalls
1. sep goes BETWEEN, end goes AFTER
The most common print-parameter confusion. sep is what separates multiple objects; end is what terminates the whole call. `print("a", "b", sep=" - ", end="!")` produces "a - b!" with no newline.
Confused roles
print("a", "b", sep="!")
"a!b\n" # newline still added
end suppresses newline
print("a", "b", sep=" ", end="!")
"a b!" # no newline
2. print returns None — do not use it in expressions
A common beginner error, especially when translating from other languages. `x = print(y)` sets x to None.
Captured None
result = print("hello") type(result)
<class 'NoneType'>
Just print
print("hello") # side effect only
3. flush=False by default; output may buffer
When writing to a pipe (or when Python decides to buffer), print output can lag behind. Progress indicators, prompts, and any "show now" use case need flush=True.
Delayed output
for i in range(5): print(".", end="") time.sleep(1) # may print nothing until end
.....
Flush each dot
print(".", end="", flush=True)
appears immediately
4. Python 2 vs Python 3
Python 2 print was a STATEMENT (`print "hello"` — no parens). Python 3 print is a FUNCTION. In Python 2, `from __future__ import print_function` unlocks the modern syntax; in Python 3, the statement form is a SyntaxError.
Py2 syntax
print "hello" # in Python 3
SyntaxError
Function form
print("hello")
works everywhere modern
When to use
Use it
- One-off scripts and prototypes
- Interactive REPL sessions
- Debugging traces (with the understanding that you will remove them)
- CLI tools where the output is the point
Reach for something else
- Application logs → use the logging module
- Structured output → json.dumps or a serialization library
- Reactive UIs — print is a side-effect, not a data flow
- Anything you want to test — logging can be captured cleanly
Notes
Complexity
O(n) in the total output length
Return
None
CPython impl
Python/bltinmodule.c :: builtin_print — writes via file.write()
Memory
Allocates the joined string, then writes
Thread-safe
Individual print calls are atomic per file, but interleaved output from multiple threads can mix
FAQ
Pass end="". By default print adds a newline at the end; end lets you replace or suppress it.
History
2.0
print was a statement — no parentheses required.
3.0
print became a function with keyword arguments (PEP 3105).
3.3
flush keyword argument added.