Python basic

Understanding the print() Function in Python

In this article, we’ll explore how the print() function works in Python. It is one of the most commonly used functions, designed to display output on the screen or another standard output device.

What is a Function?

A function is a reusable block of code that performs a specific task. It typically accepts input parameters, processes them, and produces an output.
The print() function is a built-in Python function that takes one or more values as input and displays them.

Using the print() Function

Let’s look at a simple example:

print("Hello") print("Welcome", "to", "my blog") print() print("Hope you are enjoying Python")

Output:

Hello Welcome to my blog Hope you are enjoying Python

Explanation:

  • print("Hello"): Displays the string "Hello".

  • print("Welcome", "to", "my blog"): Prints multiple strings separated by spaces.

  • print(): Called without arguments, it prints a blank line.

  • print("Hope you are enjoying Python"): Prints the provided string after the blank line.

The sep Parameter

The sep parameter defines the separator that appears between multiple values. By default, this is a single space.

Example:

print("25", "08", "2020", sep="-")

Output:

25-08-2020

Explanation:

  • The values "25", "08", and "2020" are printed with - as the separator instead of a space.

The end Parameter

The end parameter defines what is printed at the end of the output. By default, this is a newline character (\n), which moves the cursor to the next line.

Example:

print("Welcome", end="") print("to my blog") print("Hope you are enjoying Python", end="**") print("Welcome to my blog")

Output:

Welcome to my blog Hope you are enjoying Python**Welcome to my blog

Explanation:

  • print("Welcome", end=""): Prints "Welcome" without adding a newline.

  • print("to my blog"): Immediately follows the previous output on the same line.

  • print("Hope you are enjoying Python", end="**"): Ends this line with "**" instead of a newline.

  • print("Welcome to my blog"): Prints the next text on the same line after "**".

Syntax of the print() Function

print(value(s), sep=' ', end='\n', file=file, flush=flush)

Parameters:

  • value(s): One or more values to print (converted to strings).

  • sep='separator': (Optional) String inserted between values; default is a space (' ').

  • end='end': (Optional) String printed at the end; default is a newline ('\n').

  • file: (Optional) An object with a write() method, such as sys.stdout. Default is standard output.

  • flush: (Optional) If True, forcibly flushes the output buffer. Default is False.

Summary

The print() function in Python allows you to display messages or data to the screen. It provides flexibility through its optional parameters (sep, end, file, and flush). Even when no arguments are provided, print() can be used to output a blank line.

👀Note: Even though passing arguments to print() is optional, you must always use parentheses to call the function properly.


Popular Posts