The print() Function in Python

UpComing SoftWare Training Demos

🚀 *Playwright With AI Testing Demo In Telugu*
🗓️ *Date:* 28 Oct2026, 08:00 PM IST
📝 *Register Now!:*
https://www.vlrt.in/3r
👥 *Join WA Community:*
https://www.vlrt.in/3w
💡 *Course Content:*
https://www.vlrt.in/3c
▶️ *Demo Videos:*
https://www.vlrt.in/3v

🚀 *Gen AI Engineer (Production Focused)*
🗓️ *Date:* 29th Oct 2026, 06:00PM IST
📝 *Register Now!:*
https://www.vlrt.in/gr
👥 *Join WA Community:*
https://www.vlrt.in/gw
💡*Course Content:*
https://www.vlrt.in/ai
▶️ *Demo Videos:*
https://www.vlrt.in/gv

🚀 *Vulnerability Management Training Demo*
🗓️ *Date:* 28th Oct 2026, 10:00 AM IST
📝*Register Now!:*
https://www.vlrt.in/vr
👥 *Join Community:*
https://www.vlrt.in/cw
💡 *Course Content:*
https://www.vlrt.in/vc
▶️ *Demo Videos:*
https://www.vlrt.in/Vm

The print() Function in Python: Complete Guide

The print() function is Python’s built-in function for outputting data to the standard output (usually the console). Let’s explore all its arguments and capabilities in detail.

Basic Syntax

python

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

Arguments Explained

1. *objects (Positional Arguments)

The values to print. You can pass multiple items separated by commas.

Examples:

python

print("Hello")                  # Single argument
print("Hello", "World")         # Multiple arguments
print(10, 20, 30)               # Numbers
print("Sum:", 5 + 5)            # Mixed types

2. sep (Separator)

Specifies how to separate multiple objects (default is space ' ').

Examples:

python

print(1, 2, 3, sep=', ')        # Output: 1, 2, 3
print('a', 'b', 'c', sep='-')   # Output: a-b-c
print(2023, 12, 31, sep='/')    # Output: 2023/12/31

3. end (End Character)

Specifies what to print at the end (default is newline '\n').

Examples:

python

print("Hello", end=' ')         # No newline
print("World")                  # Output: Hello World

print("Loading", end='...\n')   # Custom ending
print("Done!")                  # Output: Loading... (newline) Done!

4. file (Output Destination)

Specifies where to write the output (default is sys.stdout).

Examples:

python

import sys
print("Error!", file=sys.stderr)  # Write to stderr

with open('output.txt', 'w') as f:
    print("Saving to file", file=f)  # Write to file

5. flush (Buffer Control)

Forces the output to be flushed (default is False).

Examples:

python

import time

# Without flush (buffered)
print("Loading", end='')
time.sleep(2)  # Pause - nothing appears immediately
print("Done")  # Appears after 2 seconds

# With flush (immediate)
print("Loading", end='', flush=True)
time.sleep(2)  # "Loading" appears immediately
print("Done")

Advanced Usage Examples

Formatting Output

python

name = "Alice"
age = 25
print(f"{name} is {age} years old")  # f-string (Python 3.6+)

Printing Lists/Tuples

python

numbers = [1, 2, 3]
print(*numbers, sep=' | ')  # Output: 1 | 2 | 3

Multi-line Printing

python

print("Line 1\nLine 2\nLine 3")
# Or:
print("""Line 1
Line 2
Line 3""")

Debugging with Print

python

x = 10
y = 20
print(f"{x=}, {y=}")  # Output: x=10, y=20 (Python 3.8+)

Key Points to Remember

  1. print() automatically converts all arguments to strings
  2. Multiple arguments are separated by spaces by default
  3. A newline is added at the end by default
  4. You can redirect output to files or other streams
  5. Flushing is useful for real-time progress displays

The print() function is much more powerful than it first appears, especially when you combine its various parameters for formatted output!

Similar Posts

  • Thonny: A User-Friendly Python IDE for Beginners in 2025

    Thonny is a free and open-source Integrated Development Environment (IDE) specifically designed for beginners learning Python. It provides a simple and user-friendly interface, making it an excellent choice for those new to programming. Key Features: Why Thonny is good for beginners: How to install Thonny: If you’re new to Python and looking for a user-friendly…

  • The Fractions module

    The Fractions module in Python is a built-in module that provides support for rational number arithmetic. It allows you to work with fractions (like 1/2, 3/4, etc.) exactly, without the precision issues that can occur with floating-point numbers. What Problems Does It Solve? Problem with Floating-Point Numbers: python # Floating-point precision issue print(0.1 + 0.2) # Output:…

  • Python Course content for kids

    Module 1: The Basics (Getting the Computer to Talk) Goal: Understand how to communicate with the computer and store information. Module 2: Making Decisions (Teaching the Computer to Think) Goal: Learn how to use logic to control the flow of a program. Module 3: Loops (Working Smarter, Not Harder) Goal: Understand how to automate repetitive…

  • Dictionaries

    Python Dictionaries: Explanation with Examples A dictionary in Python is an unordered collection of items that stores data in key-value pairs. Dictionaries are: Creating a Dictionary python # Empty dictionary my_dict = {} # Dictionary with initial values student = { “name”: “John Doe”, “age”: 21, “courses”: [“Math”, “Physics”, “Chemistry”], “GPA”: 3.7 } Accessing Dictionary Elements…

  • Default Arguments

    Default Arguments in Python Functions Default arguments allow you to specify default values for function parameters. If a value isn’t provided for that parameter when the function is called, Python uses the default value instead. Basic Syntax python def function_name(parameter=default_value): # function body Simple Examples Example 1: Basic Default Argument python def greet(name=”Guest”): print(f”Hello, {name}!”)…

Leave a Reply

Your email address will not be published. Required fields are marked *