Ways to use Python Print Function

Ways to use Python Print Function

The Print function

Python comes up with a print function to display the output. Before version 3.x of Python, the print was used as a statement i.e. it did not require brackets ().

Whereas, in the latest version, Python uses print as a function while strings, variables etc has to be enclosed in brackets.

If the print statement is used in the latest version, an error will be generated.

A print example with code

In our examples of Python tutorial, we used the latest method of displaying the output, i.e. using Python print as a function, as such it uses version 3.x.

Syntax of print()

Following is the syntax of print function:

Print(“string” or variable_name )

Example of using print function

Following example shows how to use the Python print function to display variable value. The first print function displays a string variable while the other displays an int variable by using the print Python function:

Python print function

See this example online

The output will be:

This is simple print statement

This will display a string variable

This is mixed string and int 12345

Print function in a for loop

Similarly, you can use the print function in the for loop to display values during the iteration.

In this example, we have used a List of Python with six elements. Then a for loop is used to iterate through the list. Inside the for loop, the print function is used to display the List elements.

See the example by clicking the link below:

See this example online

The Output:

This

is

Python

For

Loop

Example

 

Further Reading: Python User input | Python While loop