Skip to content

Python print()

Python's print() function is one of the first commands most beginners learn, yet it offers surprising depth and versatility. This guide explores the various capabilities of the print() function that can enhance your code's output formatting and readability.

Basic Print Usage

The simplest use of the print() function is to display text or values:

print("Hello, world")
# Output: Hello, world

print(2)
# Output: 2

print(2+3)
# Output: 5

You can combine text and expressions using commas:

print("2+3 =", 2+3)
# Output: 2+3 = 5

Customizing Output with Parameters

The sep Parameter

By default, print() separates multiple arguments with spaces. You can customize this separator using the sep parameter:

# Default separator (space)
print("a", "b", "c", "d")
# Output: a b c d

# Empty separator
print("a", "b", "c", "d", sep='')
# Output: abcd

# Hyphen separator
print("a", "b", "c", "d", sep='-')
# Output: a-b-c-d

# Custom word separator
print("a", "b", "c", "d", sep='data')
# Output: adatabdatacdatad

The end Parameter

By default, print() adds a newline character at the end of output. You can change this behavior with the end parameter:

print("a", "b", "c", "d", end=' ')
print("Hello world", end=' ')
print('Hi, how are you')
# Output: a b c d Hello world Hi, how are you

Combining sep and end

These parameters can be used together for powerful formatting:

print("a", "b", "c", "d", sep="\n", end='\t')
print("Hello world", end='\t')
print('Hi, how are you')
# Output: 
# a
# b
# c
# d Hello world Hi, how are you

Escape Sequence Characters

Python's print function supports various escape sequences for special formatting:

Escape Sequence Description
\n Newline
\t Horizontal tab
\\ Backslash
\' Single quote
\" Double quote
\r Carriage return
\b Backspace

Newline Example

print("Hello\nworld")
# Output:
# Hello
# world

Tab Example

print("Hello\t\t\tworld")
# Output: Hello         world

Escaping Special Characters

To print literal escape sequences, use the backslash as an escape character:

print('\\n')  # Escapes the sequence
# Output: \n

print("c:\\notebook\\tables")
# Output: c:\notebook\tables

Quote Characters

You can use different quotation marks strategically:

print("It is a beautiful day. But it's raining today")
# Output: It is a beautiful day. But it's raining today

print('He said, "Hi"')
# Output: He said, "Hi"

Or escape quotes when needed:

print('It is a beautiful day. But it\'s raining today')
# Output: It is a beautiful day. But it's raining today

print("He said, \"Hi\"")
# Output: He said, "Hi"

Carriage Return

The \r escape sequence returns to the beginning of the line and overwrites characters:

print('123456\rhello world')
# Output: hello world

Backspace

The \b escape sequence removes the character before it:

print('hello\bworld')
# Output: hellworld

Saving Output to Files

The print() function can send output directly to files:

f = open("print_file.txt", mode='a')  # 'w' for write, 'a' for append
print(2+3, file=f)
f.close()  # Always close files after use

Best Practices

  1. Use appropriate quote types (' or ") to minimize escaping
  2. Close file handles after printing to files
  3. Consider readability when combining multiple print parameters
  4. Use f-strings for complex string formatting in modern Python

Summary

The print() function is much more than a simple output tool. With its various parameters and support for escape sequences, you can create well-formatted, readable output for debugging, user interaction, or data presentation.

By mastering these features, you'll be able to communicate more effectively through your Python programs, whether you're creating simple scripts or complex applications.