Python print() Function



The Python print() function is a built-in function used to output data to the standard output device. Every running program in Python has an output area called "standard out", or "stdout". The Python print() function takes in any number of parameters and after processing, it prints results to standard out.

The output can be a string or any other object. However, the resultant object will be converted into a string first before displayed on the screen.

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.

Examples

In this section, we will see some examples of print() function −

Example 1

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 2

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 3

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 4

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 5

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