Skip to content

Module 2 · Python Syntax and Fundamentals — topic 11 of 14

print()

A deep look at print(): printing several values, the sep and end options, printing to other places with file, blank lines, and printing values of different types.

Beginner 12 min read·Lesson 25 of 306 in Python Programming

Learning objectives

  • Print multiple values with custom separators
  • Control line endings with end
  • Print empty lines and special characters

Definition

A deep look at print(): printing several values, the sep and end options, printing to other places with file, blank lines, and printing values of different types.

Introduction

print() is the function you will call more than any other while learning. It converts each value you give it to text and writes it to the screen. It has a few options that make output tidy without extra work.

Why it matters

Clear output is how a program communicates with its user, and how you see what your code is doing while you develop it. Controlling separators and line endings lets you produce neat tables, progress messages and reports.

Detailed explanation

print(*values) accepts any number of values of any type and converts each with str().

sep (default " ") is inserted between values. Use sep="" for none, sep=", " for a list style, or sep="\n" to put each value on its own line.

end (default "\n", a newline) is printed after the last value. Use end=" " to keep the next print on the same line.

print() with no arguments prints just the end text, which is an empty line.

file= sends the output somewhere other than the screen, such as an open file or sys.stderr for error messages. flush=True forces output to appear immediately.

print returns None; it is used for its effect, not for a value.

Syntax

Python
print(*values, sep=" ", end="\n", file=None, flush=False)
  • *values: zero or more values to show.
  • sep and end are keyword-only options.
  • file and flush are rarely needed but useful for logs and progress output.

Code examples

Different types in one call

Python
print("Score:", 95, 4.5, True, None)
Output
Score: 95 4.5 True None

Custom separators

Python
print("a", "b", "c", sep="")
print("2026", "11", "22", sep="/")
print("red", "green", sep="\n")
Output
abc
2026/11/22
red
green

More examples

A progress bar on one line

Python
steps = 5
print("Uploading", end=" ")
for i in range(1, steps + 1):
print("#" * i, end="|")
print()
print("Done", "100%", sep=": ")
Output
Uploading #|##|###|####|#####|
Done: 100%

Step-by-step explanation

  • end=" " keeps the cursor on the same line after “Uploading”.
  • Each loop iteration prints a growing row of # followed by |, still on the same line.
  • print() with no arguments finishes the line.
  • sep=": " joins Done and 100% with a colon and space.

Expected output

Running “A progress bar on one line” prints exactly:

TextOutput
Uploading #|##|###|####|#####|
Done: 100%

Practical applications

  • Command-line installers print progress on one line using end and flush.
  • Scripts print CSV-style output with sep="," for quick exports.
  • Error messages are printed to sys.stderr so they can be separated from normal output.

Best practices

  • Use commas to print mixed types; use f-strings for precise formatting.
  • Use sep and end instead of building strings by hand.
  • Send error messages to sys.stderr in larger scripts.

Common mistakes

Joining with + instead of commas

Avoid

print("Total: " + 250)

Prefer

print("Total:", 250)

+ needs both sides to be strings; commas let print convert the number for you.

Expecting print to return a value

Avoid

result = print(5)
print(result + 1)

Prefer

result = 5
print(result + 1)

print returns None, so result + 1 fails.

Key takeaways

  • print() accepts any number of values and converts them to text.
  • sep controls the text between values; end controls the text after them.
  • print() returns None.

Try it yourself

  1. 1
    Print the numbers 1 to 5 on one line separated by " - " using a single print call.
  2. 2
    Print a 3-line address with one print call.
    Show hint

    Use sep="\n".

Finished reading? Mark it complete to update your dashboard.

Suggested practice

1 question test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhat is printed?
Practise this topic