Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › Python › Reference › print()

Python Reference

print()

Output text or values to the console or a file. The classic Python output function.

Last updated: October 2026

Definition: print() outputs text or values to the console or a file. It converts arguments to strings, separates them with a space by default, and ends with a newline character.

🎬 Live Example

Five common print patterns. Click any button to see the output.

Input:

                
Output:

            

💡 print is a function in Python 3 — always use parentheses.

📝

Basic Syntax

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

# *objects — values to print (any number)
# sep       — separator between objects (default: space)
# end       — string appended after last object (default: newline)
# file      — output target (default: sys.stdout)
# flush     — force flush of the stream (default: False)

🎯 Parameters

Parameter Type Description
*objectsanyValues to print. Any number of arguments.
sepstrSeparator between arguments. Default: ' '
endstrString after the last argument. Default: '\n'
filefile objectOutput target. Default: sys.stdout
flushboolForce flush the stream. Default: False
1

Basic Printing

print("Hello, World!")
# Hello, World!

print(42)
# 42

print(3.14, "pi", True)
# 3.14 pi True

print()  # blank line
2

Custom Separator (sep)

print("a", "b", "c")
# a b c

print("a", "b", "c", sep="-")
# a-b-c

print("2026", "10", "11", sep="/")
# 2026/10/11

print("a", "b", "c", sep="")
# abc

Pro tip: sep="" is the fastest way to concatenate multiple values in a print.

3

Custom End Character (end)

print("Loading", end="")
print("...done")
# Loading...done

# Progress on one line
for i in range(3):
    print(i, end=" ")
# 0 1 2

Note: end replaces the default newline. Pass end="" to stay on the same line.

4

String Formatting with f-strings

name = "Alice"
age = 30

# f-string (modern, preferred)
print(f"{name} is {age} years old")
# Alice is 30 years old

# Expressions inside
print(f"{2 + 3 = }")
# 2 + 3 = 5

# Number formatting
print(f"{3.14159:.2f}")
# 3.14
5

Writing to a File

with open("output.txt", "w") as f:
    print("Line 1", file=f)
    print("Line 2", file=f)

# Equivalent to f.write("Line 1\n")
# But print adds the newline automatically

Using file= is handy when you want consistent newline handling across console and file output.

🎯 print vs write vs logging

Method Use when Notes
print()Quick output, debuggingSimple, always available
file.write()Precise file output controlNo automatic newline
logging.info()Production loggingLevels, timestamps, handlers

⚠️ Common Mistakes

❓ Frequently Asked Questions

What does print() do in Python?

print() outputs text or values to the console (or another file). It converts arguments to strings, separates them with a space by default, and ends with a newline character.

How do I print without a newline in Python?

Pass end='' to print(): print('Hello', end=''). This overrides the default newline character at the end of output.

What is the difference between sep and end in print()?

sep is the separator placed between multiple arguments (default ' '). end is what is printed after the last argument (default '\n').

How do I format strings in print()?

Use f-strings: print(f'Hello {name}'). F-strings are the modern, preferred way. Older options include .format() and % formatting.

Can print() write to a file?

Yes. Pass a file object: print('text', file=f). The file must be opened in write mode.

🎯 What's Next?

← All Python Reference