Python print() Function



The Python print() function is used to print data to the standard output device, usually on the console. The data can be a string or any other object. However, the resultant object will be converted into a string before printing it on the console or standard output device.

The print() function is one of the most commonly used built-in functions and can be used for debugging purposes also.

Syntax

The syntax of the Python print() function is as follows −

print(object, sep, end, file, flush)

Parameters

The Python print() function accepts the following parameters −

  • object − It represents an object such as a list, string, or tuple. It can accept any number of objects.

  • sep − It specifies a separator which is used for separating multiple values.

  • end − This parameter specifies what to print at the end of line. The default value is "\n".

  • file − It specifies an object with a write method. The default is sys.stdout.

  • flush − It represents a Boolean value whcih specify if the output is flushed or buffered. Default is buffered.

Return Value

The Python print() function does not return any value.

print() Function Examples

Practice the following examples to understand the use of print() function in Python:

Example: Basic Use of print() Function

The following example shows the basic usage of Python print() function. Here, we are displaying a simple string on the screen.

print("Welcome to Tutorials Point!!")

When we run above program, it produces following result −

Welcome to Tutorials Point!!

Example: print() Function With Multiple Arguments

The print() function can accept multiple items which can be strings, numbers, or any other objects. By default, the items are separated by a space. In the code below, we are displaying two different strings in a single line of output.

print("The best tutorials provided by:" "Tutorials Point!!")

Following is an output of the above code −

The best tutorials provided by:Tutorials Point!!

Example: Printing Formatted String Using print() Function

To print formatted strings, we use an f-string, which is a way of embedding expressions inside string literals using curly braces. The code below demonstrates how to embed expressions within print() function.

OrgName = "Tutorials Point"
location = "India"
print(f"{OrgName} is located in {location}.")

Output of the above code is as follows −

Tutorials Point is located in India.

Example: print() Function With 'sep' Parameter

To separate the objects, we use the "sep" parameter which defines a separator between the items. In the below example, we have used a hyphen as a separator.

print("TutorialsPoint", "is", "located", "in", "India.", sep="-")

Following is an output of the above code −

TutorialsPoint-is-located-in-India.

Example: Printing List Object Using print() Function

In the code below, a list of integers is created and then, with the help of print() function, we display them on the screen.

numericList = [66, 77, 88, 99, 111, 222]
print("Printing the list using print:", numericList)

On executing the above code, following output will be displayed −

Printing the list using print: [66, 77, 88, 99, 111, 222]
python_built_in_functions.htm
Advertisements