The print() Function in Python

UpComing SoftWare Training Demos

🚀 *Gen AI Engineer Telugu (Production Focused)*
🗓️ *Date:* 30th Sept 2026, 07:00AM 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

🚀 *Service now Admin/ Development (ITSM)FREE Demo in Telugu*
🗓️ *Date:* 30th Sept 2026, 08:00AM IST
📝 *Register Now!:*
https://www.vlrt.in/7r
👥 *Join WA Community:*
https://www.vlrt.in/7w
💡*Course Content:*
https://www.vlrt.in/7c
▶️ *Demo Videos:*
https://www.vlrt.in/7v

🚀 *Vulnerability Management Training Demo*
🗓️ *Date:* 30th Sept 2026, 09: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

  • Escape Sequences in Python

    Escape Sequences in Python Regular Expressions – Detailed Explanation Escape sequences are used to match literal characters that would otherwise be interpreted as special regex metacharacters. 1. \\ – Backslash Description: Matches a literal backslash character Example 1: Matching file paths with backslashes python import re text = “C:\\Windows\\System32 D:\\Program Files\\” result = re.findall(r'[A-Z]:\\\w+’, text) print(result) #…

  • break and continue

    In Python, break and continue are loop control statements. They allow you to alter the standard flow of a loop (whether a for loop or a while loop) based on specific conditions. Here is a detailed breakdown of how each statement works, along with examples. The break Statement The break statement is used to completely…

  • List of Basic Regular Expression Patterns in Python

    Complete List of Basic Regular Expression Patterns in Python Character Classes Pattern Description Example [abc] Matches any one of the characters a, b, or c [aeiou] matches any vowel [^abc] Matches any character except a, b, or c [^0-9] matches non-digits [a-z] Matches any character in range a to z [a-z] matches lowercase letters [A-Z] Matches any character in range…

  • Raw Strings in Python

    Raw Strings in Python’s re Module Raw strings (prefixed with r) are highly recommended when working with regular expressions because they treat backslashes (\) as literal characters, preventing Python from interpreting them as escape sequences. path = ‘C:\Users\Documents’ pattern = r’C:\Users\Documents’ .4.1.1. Escape sequences Unless an ‘r’ or ‘R’ prefix is present, escape sequences in string and bytes literals are interpreted according…

  • Mathematical Functions

    1. abs() Syntax: abs(x)Description: Returns the absolute value (non-negative value) of a number. Examples: python # 1. Basic negative numbers print(abs(-10)) # 10 # 2. Positive numbers remain unchanged print(abs(5.5)) # 5.5 # 3. Floating point negative numbers print(abs(-3.14)) # 3.14 # 4. Zero remains zero print(abs(0)) # 0 # 5. Complex numbers (returns magnitude) print(abs(3 +…

Leave a Reply

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