Python The print() Function

Display values with print(), control separators and line endings with sep and end, and send output to files or other streams.

Lesson 4 of 107Beginner12 min read

The Python print() function displays values as text. Give it any number of values separated by commas, and it converts each one to a string, puts a space between them and finishes with a newline:

Python
print("Night sky", 18)          # Night sky 18
print("Doors:", "19:30", sep=" ")

Four keyword parameters control the details: sep (what goes between values), end (what goes after the last one), file (where the text goes) and flush (whether to send it immediately). This lesson covers each one using a planetarium printing its evening schedule, and explains one point that confuses many beginners: print() shows text but returns None.

Arguments

print() accepts any number of positional arguments of any type. Each one is converted with str() before it is displayed, so numbers, booleans, lists and None can all be printed without converting them yourself:

Python
seats = 18
print("Show:", "Night sky", "| seats:", seats, "| sold out:", seats == 0)

This prints Show: Night sky | seats: 18 | sold out: False. Converting for display does not change the original values: seats is still the integer 18 afterwards, and nothing new is stored.

Printing several arguments is convenient for quick output and debugging. For carefully formatted text, such as aligned columns or prices with two decimals, build one string with an f-string and print that: print(f"{title:<12}{seats:>4}"). Called with no arguments, print() outputs an empty line.

Separators

sep is the string inserted between arguments. The default is a single space:

Python
print("Night sky", "19:30", "Dome A", sep=" | ")   # Night sky | 19:30 | Dome A
print(2026, 10, 11, sep="-")                       # 2026-10-11
print("a", "b", "c", sep="")                       # abc
print("a", "b", sep="\n")                          # a and b on separate lines

sep must be a string or None; None means "use the default space". Passing a number, such as sep=0, raises TypeError. The separator only appears between values, never before the first or after the last, so print("solo", sep="|") prints just solo.

A useful trick combines sep with unpacking: print(*items, sep=", ") prints every element of a list on one line, separated by commas, without building a string yourself.

Line endings

end is the string printed after the last argument. Its default is "\n", a newline, which is why each print() normally starts a fresh line. Change it to keep the next output on the same line:

Python
print("Doors:", end=" ")
print("19:30")                 # Doors: 19:30

for minute in range(3, 0, -1):
    print(minute, end="... ")
print("Lift-off")              # 3... 2... 1... Lift-off

end="" prints nothing at all after the value. If you remove every newline, though, the output becomes one long, unreadable line, and some terminals don't show text until a newline arrives. Make sure the final print() in a sequence ends the line, either by leaving end at its default or with a plain print().

Streams and returns

By default print() writes to standard output, sys.stdout, which is normally the terminal. The file parameter sends the text somewhere else instead: an open file, the error stream sys.stderr, or an in-memory buffer.

Python
import sys, io

print("Warning: projector offline", file=sys.stderr)

buffer = io.StringIO()
print("Night sky", 18, sep=" | ", file=buffer)
print(repr(buffer.getvalue()))   # 'Night sky | 18\n'

print() itself always returns None. Writing line = print("Night sky") displays the text and then stores None in line; it does not capture anything. To keep text, build the string first (line = f"Night sky | {seats}") or print into an io.StringIO as above.

flush=True forces the text out immediately instead of waiting in a buffer, which matters for progress messages in long-running scripts and when output is piped into another program.

Independent practice and a worked check

Task: display three schedule columns and capture one printed line with an in-memory text stream. Print a heading row and two show rows with sep=" | ", then print the first show row into an io.StringIO and display its captured text with repr().

Python
import io

print("Time", "Show", "Seats", sep=" | ")
print("18:00", "Night sky", 18, sep=" | ")
print("19:30", "Planets", 24, sep=" | ")

captured = io.StringIO()
print("18:00", "Night sky", 18, sep=" | ", file=captured)
print(repr(captured.getvalue()))

The last line prints '18:00 | Night sky | 18\n'. repr() makes the trailing newline visible, which shows that the default end was written into the buffer too. For the worked check, the keyword that changes the text between printed arguments is sep; end only controls what comes after the last one.

Reference and next practice

The official print() documentation lists every parameter. Continue with string formatting for aligned columns and decimal places, and user input for reading text back from the keyboard. Then practise with the Python exercises and Python quiz.

The print() Function Summary

  • Arguments: print converts its arguments into text for display without changing their original types.
  • Separators: sep sets the text inserted between arguments.
  • Line endings: end sets the text following the final argument.
  • Streams and returns: file selects a writable text stream, while print itself returns None.

The print() Function Examples

Each example shows the code, the output it produces and what each part does.

Example 1

The print() Function in a small program

sep and end work together in the first call; the second and third calls share one line.

Python
print("Night sky", 18, sep=" | ", end=" seats\n")
print("Doors:", end=" ")
print("19:30")
Output
Night sky | 18 seats
Doors: 19:30
Line-by-line explanation
  1. Line 1The two values are joined with " | ", and end adds " seats" plus a newline after the last one.
  2. Line 2end=" " replaces the newline with a space, so the next print continues on the same line.
  3. Line 3This call uses the default end, finishing the line.
Example 2

Printing a list and writing a schedule file

Python
shows = ["Night sky", "Planets", "Black holes"]
times = ["18:00", "19:30", "21:00"]

print(*shows, sep=", ")

with open("schedule.txt", "w", encoding="utf-8") as file:
    for show, time in zip(shows, times):
        print(time, show, sep="  ", file=file)

with open("schedule.txt", encoding="utf-8") as file:
    print(file.read(), end="")
Output
Night sky, Planets, Black holes
18:00  Night sky
19:30  Planets
21:00  Black holes
Line-by-line explanation
  1. Line 4*shows unpacks the list into three separate arguments for print().
  2. Line 8file=file writes each line into schedule.txt instead of the screen; print() still adds the newline.
  3. Line 11The file's text already ends with a newline, so end="" avoids a blank line at the end.

Common The print() Function Mistakes

Storing the result of print()

Don't
line = print("Night sky", 18)
print(line.upper())     # AttributeError: 'NoneType' object has no attribute 'upper'
Do
line = f"Night sky {18}"
print(line)
print(line.upper())

print() displays text and returns None. Build the string first if you need to keep or reuse it.

Joining text and numbers with + inside print()

Don't
seats = 18
print("Seats: " + seats)    # TypeError
Do
seats = 18
print("Seats:", seats)
print(f"Seats: {seats}")

Commas let print() convert each value for you; + requires both sides to be strings already.

The print() Function in Real Projects

print() in scripts and command-line tools

Command-line tools print normal results to standard output and warnings or errors to sys.stderr, so a user can redirect the results to a file (python report.py > out.txt) while still seeing the errors on screen. Progress indicators, such as a percentage that updates in place, use end="\r" and flush=True to rewrite the same line.

print() is also the quickest debugging tool there is. Once a program grows, though, developers usually switch to the logging module, which adds timestamps and severity levels and can be turned up or down without deleting print statements.

The print() Function FAQ

How do I print without a newline in Python?

Pass end="" (or any other string) to print(): print("Loading", end=""). The next print() then continues on the same line. Remember to finish the line later, or the following output, including your terminal prompt, will appear on the same line.

How do I print on the same line in a loop?

Use end=" " inside the loop, then a plain print() after it to end the line: for n in range(5): print(n, end=" ") prints 0 1 2 3 4. Alternatively build the text first and print once, for example print(" ".join(str(n) for n in range(5))).

What does flush=True do in print()?

Output is often held in a buffer and written in chunks for efficiency. flush=True writes the text immediately. It matters for progress messages in long tasks, output piped to other programs, and logs you want to see while a script is still running.

Is print a function or a statement?

In Python 3 it is a function, so it needs brackets: print("Hi"). In Python 2 it was a statement written without brackets, print "Hi", which is a SyntaxError in Python 3. Code without the brackets comes from old tutorials.

How do I print a variable's name and value together?

Use the = specifier in an f-string: print(f"{seats=}") prints seats=18. It works with expressions too, print(f"{seats * 2 = }"), and is available from Python 3.8.

The print() Function Exercises

Answer each one and check it instantly. If you get stuck, open the hint before the answer.

Multiple choiceeasy

Which keyword changes the text between printed arguments?

Multiple choicemedium

Which review concern is valid for the print() function?

The print() Function Quiz

2 questions to check what you learned.

  1. Which keyword changes the text between printed arguments?

    • end
    • sep
    • Python ignores the distinction
  2. Which statement correctly describes line endings?

    • end sets the text following the final argument.
    • Only the example output matters, not the rule
    • Every operation has the same behavior