The Python print() function converts its arguments to strings, writes them to the standard output separated by spaces, and ends the line with a newline. Every script uses it for quick output and debugging, and four keyword arguments change the separator, the line ending, the target and the buffering.
i = 100
print("The value of i is:", i) # The value of i is: 100
We start with the syntax and move on to sep and end, writing to stderr and files, the None return value, and better ways to print debugging values and nested data. Every example was run on Python 3.14.
1. Syntax and Return Value
The function takes any number of values plus four optional keyword arguments.
print(*objects, sep=' ', end='\n', file=None, flush=False) -> None
- objects are zero or more values; each is converted with str().
- sep is the string between the values, a space by default.
- end is the string after the last value, a newline by default.
- file is where the text goes; None means sys.stdout, the screen.
- flush forces the text out of the buffer at once when it is True.
The last four are keyword-only arguments, so we always pass them by name. The function returns None, so x = print(…) stores None, not the printed text.
result = print("hi") # prints hi, result = None
2. sep and end
The sep string goes between values and the end string after them. Both must be strings, or None for the default.
print("A", "B", "C", sep=" # ", end=" Done\n") # A # B # C Done
print(*["red", "green"], sep=", ") # red, green
To keep several prints on one line, see print without a newline.
3. Printing to stderr and Files
The file parameter accepts any object with a write() method. Error messages belong on sys.stderr, so they still show up when the normal output is redirected to a file.
import sys
print("Disk almost full", file=sys.stderr)
with open("report.txt", "w", encoding="utf-8") as f:
print("Total:", 42, file=f) # report.txt contains: Total: 42
The with block closes the file, even when an exception occurs. The file must be opened in text mode, because print() writes strings, not bytes.
4. Printing Values for Debugging
An f-string with = prints the expression and its value, and !r shows the repr(), which makes spaces and quotes visible.
name = "Lokesh "
print(f"{name=}") # name='Lokesh '
print(f"[{name!r}]") # ['Lokesh ']
For nested dictionaries and lists, pprint.pp() breaks the structure over several lines.
from pprint import pp
config = {"db": {"host": "localhost", "ports": [5432, 5433]}, "debug": True, "name": "shop"}
pp(config, width=40)
{'db': {'host': 'localhost',
'ports': [5432, 5433]},
'debug': True,
'name': 'shop'}
For messages in a long-running application, the logging module is the better tool, because it adds levels and timestamps and can be switched off without editing the code.
5. Conclusion
Most of the time we call print() with a few values and the defaults. The keyword arguments sep, end, file and flush cover the remaining cases, and f-strings with = or pprint.pp() make debugging output easier to read.
6. References
Happy Learning !!