print()

Writes values to standard output, which is the console by default.

Returns
None
On this page

Example

#
Python
name = "Emma"
points = 120

print("Hi,", name)
print(f"You have {points} points")
Resultsaved result, you can check it
Hi, Emma
You have 120 points

Definition and usage

#

The print() function converts the given values to text and writes them on one line, separated by spaces. It adds a newline character at the end, so the next call starts on a new line.

You can change the separator and the ending with the sep and end parameters. That makes it easy to print values separated by a custom character, or several calls on the same line.

The most convenient way to combine text with a variable value is an f-string: print(f"You have {points} points").

Syntax

#
Syntax
print(*objects, sep=' ', end='\n', file=None, flush=False)

Parameters

#
  • *objects

    any values

    The values to print. Each one is converted to text as if by str().
  • sep

    str, default ' '

    The text inserted between values.
  • end

    str, default '\n'

    The text appended at the end. An empty string end="" makes the next print() write on the same line.
  • file

    a file object

    Where to write. Defaults to sys.stdout, but you can also pass an open file or sys.stderr.
  • flush

    bool, default False

    When True, the text reaches the output immediately, without buffering.

More examples

#
A custom separator and ending
Python
print("HTML", "CSS", "JavaScript", sep=" → ")

for i in range(3):
    print(i, end=" ")
print("done")
Resultsaved result, you can check it
HTML → CSS → JavaScript
0 1 2 done
Writing to a file
Python
with open("log.txt", "w", encoding="utf-8") as file:
    print("Program started", file=file)
Result
Click Run to see the result.

The text goes into the log.txt file instead of the console.

Good practices

#
  • print() is fine to start hunting for bugs, but in bigger programs the logging module works better.
  • Calling print() with no arguments prints an empty line.

Related entries

#

Spotted a mistake or missing an example? Write to us.