Python Reference
print()
Output text or values to the console or a file. The classic Python output function.
Last updated: October 2026
Definition: print() outputs text or values to the console or a file. It converts arguments to strings, separates them with a space by default, and ends with a newline character.
🎬 Live Example
Five common print patterns. Click any button to see the output.
💡 print is a function in Python 3 — always use parentheses.
Basic Syntax
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
# *objects — values to print (any number)
# sep — separator between objects (default: space)
# end — string appended after last object (default: newline)
# file — output target (default: sys.stdout)
# flush — force flush of the stream (default: False)
🎯 Parameters
| Parameter | Type | Description |
|---|---|---|
*objects | any | Values to print. Any number of arguments. |
sep | str | Separator between arguments. Default: ' ' |
end | str | String after the last argument. Default: '\n' |
file | file object | Output target. Default: sys.stdout |
flush | bool | Force flush the stream. Default: False |
Basic Printing
print("Hello, World!")
# Hello, World!
print(42)
# 42
print(3.14, "pi", True)
# 3.14 pi True
print() # blank line
Custom Separator (sep)
print("a", "b", "c")
# a b c
print("a", "b", "c", sep="-")
# a-b-c
print("2026", "10", "11", sep="/")
# 2026/10/11
print("a", "b", "c", sep="")
# abc
Pro tip: sep="" is the fastest way to concatenate multiple values in a print.
Custom End Character (end)
print("Loading", end="")
print("...done")
# Loading...done
# Progress on one line
for i in range(3):
print(i, end=" ")
# 0 1 2
Note: end replaces the default newline. Pass end="" to stay on the same line.
String Formatting with f-strings
name = "Alice"
age = 30
# f-string (modern, preferred)
print(f"{name} is {age} years old")
# Alice is 30 years old
# Expressions inside
print(f"{2 + 3 = }")
# 2 + 3 = 5
# Number formatting
print(f"{3.14159:.2f}")
# 3.14
Writing to a File
with open("output.txt", "w") as f:
print("Line 1", file=f)
print("Line 2", file=f)
# Equivalent to f.write("Line 1\n")
# But print adds the newline automatically
Using file= is handy when you want consistent newline handling across console and file output.
🎯 print vs write vs logging
| Method | Use when | Notes |
|---|---|---|
print() | Quick output, debugging | Simple, always available |
file.write() | Precise file output control | No automatic newline |
logging.info() | Production logging | Levels, timestamps, handlers |
⚠️ Common Mistakes
- Forgetting parentheses.
print "hi"is Python 2 syntax. Python 3 requiresprint("hi"). - Confusing sep and end. sep is between args; end is after the last arg.
- Expecting print to return a value. It returns
None. - Forgetting newline. print adds one by default — pass
end=""to skip it. - Using print for production logging. Use the
loggingmodule instead. - Mixing strings and ints without conversion. Use f-strings or
str().
❓ Frequently Asked Questions
What does print() do in Python?
print() outputs text or values to the console (or another file). It converts arguments to strings, separates them with a space by default, and ends with a newline character.
How do I print without a newline in Python?
Pass end='' to print(): print('Hello', end=''). This overrides the default newline character at the end of output.
What is the difference between sep and end in print()?
sep is the separator placed between multiple arguments (default ' '). end is what is printed after the last argument (default '\n').
How do I format strings in print()?
Use f-strings: print(f'Hello {name}'). F-strings are the modern, preferred way. Older options include .format() and % formatting.
Can print() write to a file?
Yes. Pass a file object: print('text', file=f). The file must be opened in write mode.