Beginner   Chapter 19 · 25 min read · Module 1: Python Fundamentals

Print Function

Learning objectives
  • After this chapter you can print one or many values of any type.
  • You can change the separator and the line ending with the sep and end arguments.
  • You can use escape characters such as \n, \t and \\ inside strings.
  • You can send output to standard error or a file object with the file argument.
  • You can explain that print() returns None and prints the str form of values.

The print() function is the first tool every Python learner uses, and it stays useful for years. It is the simplest way to see what your program is doing, to show results to a user, and to find mistakes while you work. Because it is used so often, it is worth learning its full set of options.

Besides plain text, print() can show numbers, lists, dictionaries and any other value. It has keyword arguments that change the separator between values, the ending of the line, and the place where the output goes. Together with escape characters and f-strings, these options let you control the look of your output exactly.

This chapter walks through each feature with small, runnable examples. Every output you see was produced by real Python, so you can type the same code and get the same result.

Printing different values

print() accepts any number of values separated by commas. Each value is turned into text using str(), then the pieces are joined with a separator and followed by an ending. By default the separator is one space and the ending is a new line. A call with no values at all, print(), prints just an empty line.

Example 1: many values and types
print("Python", 3, 3.5, True, None)
print([1, 2], (3, 4), {"a": 1})
print()
print("after the blank line")
Python 3 3.5 True None [1, 2] (3, 4) {'a': 1} after the blank line

Notice that a string inside a list is shown with quotes, such as ['a'], but a plain string is shown without quotes. When print shows a list it uses the repr form of each item, which is a form meant for programmers.

The sep argument

The keyword sep changes what goes between the values. You can set it to a comma, a dash, an empty string or even a newline. Keyword arguments must come after the values and must be written with their names.

The end argument

The keyword end changes what is printed after the last value. The default is "\n", which is why each call starts on a new line. If you set end="", the next print continues on the same line. This is helpful when you build a line of output piece by piece.

Example 2: sep and end
print("2025", "01", "15", sep="-")
print("a", "b", "c", sep="")
print("one", "two", sep="\n")
for n in range(1, 5):
    print(n, end=" ")
print()
print("Loading", end="...")
print("done")
2025-01-15 abc one two 1 2 3 4 Loading...done

Escape characters

An escape sequence begins with a backslash and stands for a character that is hard to type. The most useful ones are \n for a new line, \t for a tab, \\ for one backslash, \" for a double quote and \' for a single quote. A raw string, written with an r before the quote, keeps backslashes as they are, which is useful for file paths in Windows and for regular expressions.

Example 3: escapes and raw strings
print("Line one\nLine two")
print("Name\tAge")
print("Asha\t21")
print("She said \"hi\"")
print("C:\\data\\file.txt")
print(r"C:\data\new.txt")
Line one Line two Name Age Asha 21 She said "hi" C:\data\file.txt C:\data\new.txt
EscapeMeaning
\nnew line
\ttab
\\a backslash
\"a double quote
\'a single quote

Repeating, joining and long text

Multiplying a string by a number repeats it, so "-" * 20 makes a line of twenty dashes. This is a quick way to draw borders in a console report. Triple-quoted strings, written with three quotes, keep line breaks exactly as you type them, so they are good for multi-line messages. To print list items with your own separator, you can unpack them with a star, as in print(*items, sep=", ").

Example 4: borders, triple quotes and unpacking
print("=" * 15)
print("Report")
print("=" * 15)
print("""Roses are red,
Violets are blue.""")
items = ["tea", "milk", "sugar"]
print(*items, sep=", ")
print(*range(1, 6))
=============== Report =============== Roses are red, Violets are blue. tea, milk, sugar 1 2 3 4 5

Debugging with f-strings

Since Python 3.8 you can write f"{x=}" inside an f-string. Python prints the name of the expression, an equals sign and its value. This saves typing when you inspect variables while you search for a bug. For example, print(f"{total=}") might show total=42.

Example 5: showing names and values
x = 7
y = [1, 2]
print(f"{x=}")
print(f"{y=}")
print(f"{x * 2 = }")
result = print("hello")
print(result)
x=7 y=[1, 2] x * 2 = 14 hello None

The last two lines of that example show a fact that surprises many beginners: print() itself returns None. It shows text on the screen, but it does not give back that text. If you need the text as a value, build it with str() or an f-string and store it.

Where does the output go?

By default print writes to standard output, called sys.stdout. Programs also have standard error, sys.stderr, which is meant for error messages and is kept apart from normal results. You choose with the file argument: print("problem", file=sys.stderr). You can also pass a file object opened for writing. The flush=True argument forces the text to appear at once instead of waiting in a buffer, which matters in long-running programs that show progress.

Writing messages for developers to standard error keeps them out of the normal output, so another program that reads your output does not get confused.

Python syntax

Syntax
print(value1, value2, ..., sep=" ", end="\n", file=sys.stdout, flush=False)
Syntax
print(*iterable, sep=", ")
Syntax
print(f"{variable=}")

Real-world applications

  • Showing results and progress to the user.
  • Quick debugging of variables and flow.
  • Formatting tables and reports in a console.
  • Writing logs to the error stream or a file.
  • Printing multi-line banners and menus.

Common mistakes

Mistake: Writing print "hello" without parentheses, as in old Python 2 code.
Fix: In Python 3 print is a function: print("hello").
Mistake: Passing sep or end as a positional value, such as print("a", "-", "b") and expecting a separator.
Fix: Use keyword names: print("a", "b", sep="-").
Mistake: Forgetting that end="" means no newline, so later output sticks to the same line.
Fix: Add a plain print() when the line is complete.
Mistake: Using a single backslash in a Windows path such as "C:\new\table".
Fix: Use a raw string r"C:\new\table" or double the backslashes.
Mistake: Storing the result of print, as in text = print("x"), and expecting text.
Fix: print returns None. Build the string first, then print it.

Important tips

  • Use f"{name=}" while debugging.
  • Use sep and end instead of building strings by hand.
  • Use print(*items) to show list items without brackets.
  • Send diagnostics to sys.stderr so normal output stays clean.

Key points summary

  • print() can take any number of values of any type.
  • sep controls the text between values; end controls the text after.
  • Escape sequences start with a backslash.
  • Raw strings keep backslashes unchanged.
  • print() returns None.
  • file= and flush= control where and when output appears.

Exam and interview points

  • Default sep is a space and default end is a newline.
  • print() with no arguments prints an empty line.
  • print returns None.
  • \t and \n are single characters.
  • A raw string cannot end with an odd number of backslashes.

Quick revision

  • print(a, b, sep='-', end='!')
  • \n new line, \t tab, \\ backslash.
  • r'..' raw string.
  • print(*list) unpacks.
  • f'{x=}' debug display.

Frequently asked questions

How do I print without a new line at the end?

Use end="", for example print("Hi", end=""). The next output then continues on the same line.

How can I print a quotation mark inside a string?

Use the other kind of quote around the string, like 'She said "hi"', or escape it with a backslash.

What is the difference between print(x) and print(repr(x))?

print(x) shows the friendly str form. repr shows a precise form, for example with quotes around text.

Why does my output appear late?

Output can wait in a buffer. Use print(..., flush=True) to show it immediately.

Can print write into a file?

Yes. Open a file for writing and pass it as file=, for example print("text", file=f).

Practise this chapter

21 practice questions Take the chapter quiz

Related chapters