Skip to main content

How to Print Output in Python with print

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

print is the first thing most Python programs use: it writes values to the terminal, each call on its own line. Two optional settings, sep and end, control what goes between the values and what comes after them.

Syntax​

Syntax
print(<value>, <value>, sep="<separator>", end="<ending>")
PartWhat it does
<value>, <value>What to print. Each is turned into text the way str() would.
sep="<separator>"Optional. Goes between the values. The default is one space.
end="<ending>"Optional. Goes after the last value. The default is a new line, "\n".

The Python documentation for print: "Print objects to the text stream file, separated by sep and followed by end. sep, end, file, and flush, if present, must be given as keyword arguments."

The Example​

The example prints a sentence, a label with a number, three letters joined by hyphens, and two calls that end up on one line.

Prerequisites​

  • uv and Python. This guide uses uv 0.13.10 with Python 3.15.2.

Step 1: Write the Code​

Create a file named main.py:

main.py, which prints Hello from Python, prints Total: with 42, prints a, b and c with sep set to a hyphen, prints No new line with end set to an empty string, and then prints an exclamation mark.

main.py
print("Hello from Python")
print("Total:", 42)
print("a", "b", "c", sep="-")
print("No new line", end="")
print("!")

Step 2: Run It​

Terminal
uv run python main.py
Output
Hello from Python
Total: 42
a-b-c
No new line!

A zsh terminal where uv run python main.py prints Hello from Python, Total: 42, a-b-c and No new line!

  • "Total:", 42 printed with a space between them, the default sep.
  • sep="-" put hyphens between a, b and c.
  • end="" stopped the new line, so the next print continued on the same line.

The command is the same in zsh, bash and PowerShell.

Printing an Error Message​

print writes to standard output. Messages about problems belong on standard error, where they stay visible when the output is redirected to a file:

To standard error
import sys

print("Warning: 2 rows were skipped.", file=sys.stderr)

Common Mistakes​

MistakeWhat happensDo this instead
Writing print "Hello"SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?Write print("Hello"): print is a function
Joining a number with +, as in "Total: " + 42TypeError: can only concatenate str (not "int") to strPass it as a separate value: print("Total:", 42)
Expecting print to return the textIt returns NoneBuild the string first, then print it

Troubleshooting​

SymptomLikely causeFix
Two outputs run together on one lineAn earlier print used end=""Leave end at its default, or print an empty print()
Error messages end up in a redirected fileThey were printed to standard outputPrint them with file=sys.stderr

FAQ​

How do I print a blank line?​

Call print() with nothing in it. It prints just the end, a new line.

Why is some output delayed when I pipe it?​

Python buffers output that goes to a file or a pipe. print(..., flush=True) writes it immediately.

Conclusion​

print writes its values with spaces between them and a new line after. Change those with sep and end, and send problems to sys.stderr with file=.

Additional Resources​

  • print(), in the Python documentation.