Python / print() / Output Values as Text

Python’s print() converts values to text and normally writes them to standard output. It inserts spaces between multiple values and a newline at the end; sep, end, file, and flush control this behavior. It returns None, rather than the displayed text.

Basic output and quotation marks

The signature is print(*objects, sep=' ', end='\n', file=None, flush=False). *objects allows multiple values. Without an explicit file, standard output, sys.stdout, usually sends text to a terminal or IDE run window. See the print() reference for argument rules.

Use Python 3. Save each complete example in UTF-8 as print_example.py and run python print_example.py, or python3 where appropriate. Replace the contents to run each example independently.

print("Hello")
print('Hello')
print("'Hello'")
print('"Hello"')
print("count", 3, True, None)

Expected output:

Hello
Hello
'Hello'
"Hello"
count 3 True None

Single and double quotes both delimit strings and are not themselves printed. To include quotes, surround them with the other kind as shown. Multiple arguments let you print numbers and strings without concatenating them with +. Calling print() without values outputs only its default newline.

sep separates values; end finishes the call

sep appears between arguments of one call. end follows the last value of that call. Use end="" to omit the newline, or end=" " to connect the next output with a space.

print("A", "B", sep="-", end=" | ")
print("C")
values = ["A", "B"]
print(values, sep="-")
print(*values, sep="-")
A-B | C
['A', 'B']
A-B

The first call writes A-B | and the next adds C on the same line. A list supplied as one argument prints its representation; sep does not change its internal formatting. Unpack *values to pass elements separately. Omit an option to restore its default behavior.

Formatting is separate from the return value

Build a string when you need decimal places or a sentence layout. This example uses the f-string format :.2f to display two decimal places.

price = 12.5
quantity = 3
message = f"Total: {price * quantity:.2f}"
result = print(message)
print(result)
Total: 37.50
None

message holds the text; result receives None, not the displayed result. Use return when a function needs to pass a calculated value to other code, and let its caller decide when to display it. Formatting two decimal places does not change the original numeric value’s precision.

Write a UTF-8 text file with file

Pass a writable text file object as file. This complete example writes and reads report.txt inside a temporary folder, avoiding existing files and cleaning up afterward. It needs no additional packages.

from pathlib import Path
from tempfile import TemporaryDirectory

with TemporaryDirectory() as folder:
    path = Path(folder) / "report.txt"
    with path.open("w", encoding="utf-8") as output:
        print("Alpha", 10, file=output)
    with path.open("a", encoding="utf-8") as output:
        print("Beta", 20, file=output)
    print(path.read_text(encoding="utf-8"), end="")
Alpha 10
Beta 20

Mode "w" starts a new file or clears an existing one; "a" appends. Each with block closes its file. The final end="" avoids adding a newline to those already read from the file. Check paths, write permissions, and file modes and encoding before adapting this to a real file.

Capture text and understand TypeError

StringIO behaves like a text file in memory. To reuse a printing function, supply file=output and call getvalue() before closing it. An f-string is more direct if you only need to construct one sentence.

from io import BytesIO, StringIO

with StringIO() as output:
    print("A", "B", sep="-", file=output)
    text = output.getvalue()
print(repr(text))

try:
    print("A", "B", sep=1)
except TypeError:
    print("TypeError: sep")

with BytesIO() as output:
    try:
        print("text", file=output)
    except TypeError:
        print("TypeError: binary stream")
'A-B\n'
TypeError: sep
TypeError: binary stream

The \n revealed by repr() is the captured newline. sep and end must be strings or None for their defaults; a number is invalid. BytesIO is a binary stream and cannot receive print()’s text writes. Encode text explicitly and use a binary write API for binary data.

Use flush for progress without a newline

Output may be buffered before delivery. Progress without newlines and output redirected to files or pipes can appear at unexpected times. flush=True requests that the output destination flush its buffer.

for number in range(3):
    print(number, end=" ", flush=True)
print("done")
0 1 2 done

The final characters match the output above, but update timing depends on the terminal, IDE, and destination. flush neither adds a newline nor clears the screen, and it does not guarantee persistent disk storage. Keep defaults for ordinary output and request flushing where intermediate updates matter.