Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

print: output text, not a function result

print writes text representations of objects to a stream. It returns None, so displaying a value does not replace returning it from your own function.

Version: Python 3.14

Syntax

print(*objects, sep=' ', end='\n', file=None, flush=False)

You can pass several objects: print("Python", 3) writes them with a space between them. sep sets the separator and end the ending for the whole call. Pass these options by name. With no objects, print() writes only the ending, a newline by default.

The assignment result = print(7) displays 7 but stores None in result. If your function must pass a result to further calculations, use return. An f-string can build text without printing it.

file selects a text stream with a write method; standard output is the default. A binary stream cannot accept this text without an appropriate conversion. flush=True requests flushing the buffer; merely writing a newline does not guarantee this in every environment.

The example collects output in StringIO, an in-memory text stream, and displays the text separately from the returned None. It opens no file. sep and end must be strings or None; a number used as a separator raises TypeError.

Examples

from io import StringIO

stream = StringIO()
result = print("Python", 3, sep=":", end="!", file=stream)
print(stream.getvalue())
print(result)

Result

Python:3!
None

Sources