The print() Function

Python's built-in function for displaying output โ€” what it can print, and how sep and end control the formatting.

Overview

print() is Python's built-in function for displaying output to the console (or another stream). This note covers what kinds of values it can print, and its keyword arguments sep and end, which control how multiple values are separated and what's printed after the last value.

Key Concepts

Detailed Explanation

1. Printing Basic Data Types

print() works with any single value, regardless of type:

print("Hello World")   # -> Hello World      (string)
print(6)                # -> 6                (integer)
print(5.6)               # -> 5.6              (float)
print(False)             # -> False            (boolean)
Note: Python automatically converts the value to its string representation for display โ€” you don't need to manually convert types before printing.

2. Printing Multiple Values

You can pass multiple comma-separated values to print(), and it will print them all on one line, separated by a space by default:

print("India", "Pakistan", "Nepal", "Srilanka")
# -> India Pakistan Nepal Srilanka

Multiple values don't need to be the same type:

print("India", 5, True)
# -> India 5 True

3. The sep Parameter โ€” Custom Separators

By default, print() joins multiple values with a space. The sep parameter lets you change this separator to any custom string.

print("India", "Pakistan", "Nepal", "Srilanka", sep='/')
# -> India/Pakistan/Nepal/Srilanka

print("India", "Pakistan", "Nepal", "Srilanka", sep='-')
# -> India-Pakistan-Nepal-Srilanka

How it works: sep is inserted between every pair of values passed to print() โ€” it has no effect if you only pass a single value.

4. The end Parameter โ€” Controlling Line Endings

By default, every print() call ends with a newline ('\n'), which is why consecutive print() calls each start on a new line:

print("Hello")
print("World")
# -> Hello
# -> World

Changing end to something other than '\n' (e.g., a space) makes the next print() output continue on the same line:

print("Hello", end=' ')
print("world")
# -> Hello world

How it works: end replaces the default trailing newline with whatever string you specify. Here, end=' ' means "add a space instead of moving to a new line," so the next print's output appears right after it on the same line.

5. Full print() Signature

Inspecting print()'s docstring (e.g., via Jupyter's tooltip/inspector) reveals its full signature:

print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
ParameterDefaultMeaning
value, ...โ€”One or more values to print
sep' ' (space)String inserted between multiple values
end'\n' (newline)String appended after the last value
filesys.stdoutA file-like object (stream) to write to; defaults to the console
flushFalseWhether to forcibly flush the output stream immediately
Prints the values to a stream, or to sys.stdout (the console) by default.

print itself is described as builtin_function_or_method โ€” it's a function built directly into Python, not something you need to import.

Commands / Syntax Summary

# Basic printing
print("Hello World")

# Multiple values (default separator = space)
print("India", "Pakistan", "Nepal", "Srilanka")

# Custom separator
print("India", "Pakistan", "Nepal", "Srilanka", sep='/')

# Custom end (keeps output on the same line)
print("Hello", end=' ')
print("world")

Important Points

Things to Remember

Quick Revision

print() displays values to the console. Pass multiple values separated by commas to print them on one line (joined by sep, default a space). Every print() call ends with a newline by default (end='\n'), which is why successive prints appear on separate lines โ€” override end (e.g., end=' ') to keep output on the same line instead. Full signature: print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False).