Hit enter after type your search item

Python 3 print function

The print function in Python

The print function in Python is used to display the output of variables: strings, lists, tuples, ranges, etc.

Before version 3.x of Python, the print was taken as a statement. However, from Python 3.x, the print acts as a function.

That also means you need to enclose the objects in parenthesis:

Syntax of using Python print function

Following is the syntax for using the print function:

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


  • The object can be strings, lists, tuple etc.
  • The sep = ‘’ parameter specifies a space between multiple objects. You may use other than space by using this parameter.
  • The end=’\n\’ means in each call, the print function will end at a newline. A demo is shown later in this tutorial, on how you can change that.
  • The file=sys.stdout specifies where the print function should send the output. The default value is stdout that you may change, for example to an external file.
  • In Python 3.3 the flush keyword argument is added that specifies whether to flush or not the output stream. The default value is false.

See the following examples where I will use the print function with string and int variables along with other data types like tuples, lists etc. I will also show you using different parameters of the print function.

An example of displaying simple string by print function

By using the print Python function, a simple string is displayed in this example:

The code for printing the output:


Python print

Displaying a string variable by Python print

A string variable is created and displayed by using the print function:

The code:


print string

Displaying multiple values separated by commas

In print function, you may use any number of values/variables that are separated by commas. Each value is separated by space, see the following example:

The code:


print multiple values

You can see, three string variables are displayed by using print function. Each variable is separated by a comma. In the output, space is added automatically. This is due to the parameter sep = ‘’, as shown in the syntax of the print function.

Displaying numeric variable example

Similarly, you may use the numeric variables in print function to display its values. Again, you can use a comma with variables for displaying the arbitrary number of values. See this example:

Python porgram:


print integers

You can see, the int variable is displayed alone as well as with a string by using print function. A blank space is also added at the second output as displaying both string and integer value.

The Python print function call ends at newline example

As shown in the syntax of the print function, the default value of ‘end = \n’ i.e. a newline. That means the print function ends with a newline in each call. To test that, I have created a list of five numbers in this example.

A for loop is used for iteration through the list elements. In each iteration, the current value in the list is displayed by using the print function. See the code and output:

The code for creating and displaying list elements:


print list

You see, in each iteration, the value is displayed in a new line. You can change this as shown in the example below.

An example of changing end parameter rather using the newline

In this example, a full stop and space (. ) are used in the end parameter of the print function. A range is created and a for loop is used to demonstrate this:

The following code is used for changing the default newline:


print range

You may use other arbitrary string in the end parameter, as per requirement e.g. end = “:”, a comma and so on.

An example of displaying tuple by print function

Just like string or int variables, list etc. the tuple element can be displayed by using the print function. See the following example where a tuple is created with five elements. After that, the print function is used to display its items:



print tuple

This div height required for enabling the sticky sidebar