print() function

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 is one of the most commonly used built-in functions in Python. Its primary job is to take data (like text, numbers, or variables) and display it on your screen (the console).

The Anatomy of print()

The complete syntax for the print() function looks like this:

Python

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

While it looks complex, you only need to use the parts you need. Here is a breakdown of what each parameter does:

  • *objects: The items you want to print. You can print as many objects as you want, separated by commas.
  • sep: How to separate multiple objects. The default is a single space (' ').
  • end: What to place at the very end of the printed line. The default is a newline character ('\n'), which is why each print() statement usually starts on a new line.
  • file: Where to send the output. The default is the standard output screen (sys.stdout), but you can use this to print directly into a text file.
  • flush: A boolean (True or False) that dictates whether to forcibly flush the stream. (Usually left as False).

Examples of Using print()

1. Basic Output

The simplest way to use print() is to pass a single string or number.

Python

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

# Output:
# Hello, World!
# 42

2. Printing Multiple Items (*objects)

You can print multiple items in a single print() statement by separating them with commas. By default, Python will automatically insert a space between them.

Python

name = "Alice"
age = 13
print("My name is", name, "and I am", age, "years old.")

# Output:
# My name is Alice and I am 13 years old.

3. Changing the Separator (sep)

If you don’t want a space between your items, you can change the sep parameter. This is highly useful for formatting dates, times, or file paths.

Python

# Separating with a hyphen
print("apple", "banana", "cherry", sep="-")

# Separating with nothing (an empty string)
print("S", "u", "p", "e", "r", sep="")

# Output:
# apple-banana-cherry
# Super

4. Changing the Ending (end)

By default, every print() function ends by moving the cursor to the next line. If you want consecutive print() functions to display on the same line, you can change the end parameter.

Python

print("Loading", end="...")
print("Complete!")

# Output:
# Loading...Complete!

5. Combining sep and end

You can use multiple optional parameters in the same function call to get exactly the formatting you want.

Python

print("1", "2", "3", sep=" -> ", end=" GO!\n")

# Output:
# 1 -> 2 -> 3 GO!

6. Modern Printing: f-strings

While using commas to print multiple items works, modern Python (version 3.6+) uses f-strings (formatted string literals) as the standard way to mix variables and text. It makes the print() function much easier to read.

You simply place an f before the opening quote and put your variables inside curly brackets {}.

Python

item = "coffee"
price = 4.99

# Without f-string:
print("Your", item, "costs $", price, sep="")

# With f-string (Cleaner and easier to read):
print(f"Your {item} costs ${price}")

# Output for both:
# Your coffee costs $4.99

Similar Posts

  • Python Calendar Module

    Python Calendar Module The calendar module in Python provides functions for working with calendars, including generating calendar data for specific months or years, determining weekdays, and performing various calendar-related operations. Importing the Module python import calendar Key Methods in the Calendar Module 1. calendar.month(year, month, w=2, l=1) Returns a multiline string with a calendar for the specified month….

  • Finally Block in Exception Handling in Python

    Finally Block in Exception Handling in Python The finally block in Python exception handling executes regardless of whether an exception occurred or not. It’s always executed, making it perfect for cleanup operations like closing files, database connections, or releasing resources. Basic Syntax: python try: # Code that might raise an exception except SomeException: # Handle the exception else:…

  • Object: Methods and properties

    🚗 Car Properties ⚙️ Car Methods 🚗 Car Properties Properties are the nouns that describe a car. They are the characteristics or attributes that define a specific car’s state. Think of them as the data associated with a car object. Examples: ⚙️ Car Methods Methods are the verbs that describe what a car can do….

  • Functions Returning Functions

    Understanding Functions Returning Functions In Python, functions can return other functions, which is a powerful feature of functional programming. Basic Example python def outer(): def inner(): print(“Welcome!”) return inner # Return the inner function (without calling it) # Calling outer() returns the inner function f = outer() # f now refers to the inner function…

  • positive lookahead assertion

    A positive lookahead assertion in Python’s re module is a zero-width assertion that checks if the pattern that follows it is present, without including that pattern in the overall match. It is written as (?=…). The key is that it’s a “lookahead”—the regex engine looks ahead in the string to see if the pattern inside…

  • Instance Variables,methods

    Instance Variables Instance variables are variables defined within a class but outside of any method. They are unique to each instance (object) of a class. This means that if you create multiple objects from the same class, each object will have its own separate copy of the instance variables. They are used to store the…

Leave a Reply

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