Skip to content
Tutorials Center
My learning

Topics

Python Reference ยท Built-in Functions

Python print() function

Reference for Python print(): print several values, change sep and end, write to a file or stderr, flush output and format text with f-strings.

The built-in print() function converts its arguments to text with str(), joins them with a separator, adds an ending (a newline by default) and writes the result to a stream, normally standard output. It is the first function most people learn and the simplest debugging tool.

print() always returns None. It never changes the values it prints, and it accepts any number of positional arguments of any type.

Python
print("Total:", 42)          # Total: 42
print("a", "b", sep="-")     # a-b
print("loading", end="...")  # no newline afterwards

Try it

Python Separators and endings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Unpacking, f-strings and other streams

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Syntax

Python
print(*objects, sep=" ", end="\n", file=sys.stdout, flush=False)
Name Description
*objects Zero or more values to print. Each is converted with str().
sep String placed between values. Default is one space. None means the default.
end String written after the last value. Default is a newline.
file Any object with a write(string) method. Default is sys.stdout.
flush If True, flush the stream right away instead of waiting for buffering.

Return value: None.

Raises: TypeError for a wrong type of sep/end (they must be strings or None) or an unknown keyword argument; any exception the stream raises when writing.

Usage notes

  • sep and end are keyword-only. print("a", "b", "-") prints three items; you must write sep="-".
  • No arguments prints just a newline, which is the usual way to add a blank line.
  • Strings are printed as-is, other objects through str(). Use repr() or the !r conversion in an f-string to see quotes and escapes when debugging.
  • Print many items from a list with print(*items); with sep="\n" you get one per line.
  • Redirecting. file=sys.stderr sends diagnostics to the error stream; a file object from open() writes to disk; an io.StringIO captures output in memory.
  • Buffering. Output to a terminal is usually line-buffered, but to a pipe or file it is block-buffered. Use flush=True for progress indicators or when another process reads your output live.
  • Progress on one line: print(f"\r{percent}%", end="", flush=True).
  • Not a logger. For real applications prefer the logging module, which adds levels, timestamps and destinations.
  • Python 2 is gone. print "text" without parentheses is a SyntaxError in Python 3.
  • Encoding. Characters that the output encoding cannot represent raise UnicodeEncodeError; this mostly affects old Windows consoles and redirected output.