Python print Function with Examples

In Python, the print function serves the purpose of displaying the specified object on the screen or other standard output devices.

Python print Function Syntax

It has the following syntax:

Example

print(object(s), sep=separator, end=end, file=file, flush=flush)

Parameters

It has the following parameters:

  • object(s): It is an object to be printed. The Symbol * indicates that there may be more than one object.
  • sep='separator' (optional): The objects are separated by sep. The default value of sep is ' '.
  • end='end' (optional): it determines which object should be print at last.
  • file (optional): - The file must be an object with write(string) method. If it is omitted, sys.stdout will be used which prints objects on the screen.
  • flush (optional): If True, the stream is forcibly flushed. The default value of flush is False.
  • Return

It does not return any value.

Different Examples for Python print Function

In this section, we will explore various instances of the Python print function.

Python print Function Example 1

The following example demonstrates how the print function operates in Python.

Example

print("Python is programming language.")

x = 7

# Two objects passed

print("x =", x)

y = x

# Three objects passed

print('x =', x, '= y')

Output:

Output

Python is programming language.

x = 7

x = 7 = y

Explanation:

In the code presented above, the only argument being supplied to the print function across all three print statements is the objects parameter.

The end parameter '\n' (newline character) is utilized to ensure that the output appears on a new line, which is the default behavior. As observed, every print statement outputs its result on a separate line.

When the file is designated as sys.stdout, the output is displayed on the monitor.

In this case, the flush parameter is set to False, indicating that the stream will not be flushed immediately.

Python print Function Example 2

The following illustration demonstrates the use of the print function while utilizing the separator and end parameters.

Example

x = 7

print("x =", x, sep='00000', end='\n\n\n')

print("x =", x, sep='0', end='')

Output:

Output

a =000007

a =07

Input Required

This code uses input(). Please provide values below: