Display output in Python | print() function in Python

Preview

print() function in Python

We can use print() function in Python to display some message or output on Python shell.

The Syntax of print function is:

print(EXPRESSION)

Here EXPRESSION is the variable of any data type or string value that we want to display.

In previous versions of Python there was no need of parenthesis with print() function but in Python version 3 , it is mandatory to use pair of parenthesis with print() function.

Examples:

>>>print (“Welcome to Python”) # It is acceptable in Python version 3

print() automatically inserts a new line character at the end.



1. Displaying string values using print()

We can use print() function in Python shell to display string values as follows:

You can see that, I have used print() function at >>> prompt twice as:

  1. String value is taken within pair of double quotes.

>>> print(“Welcome to Python”)

Output:

Welcome to Python

 

  1. String value is taken within pair of single quotes.

>>> print(‘Welcome to Python’)

Output:

Welcome to Python



2. Displaying numeric values using print function()

We can use print() function in Python shell to display numeric values as follows:

I have used print() function at >>> prompt thrice as:

1. Integer value is displayed using print() function

>>>print(10)

Output:

10

2. float value is displayed using print() function

>>>print(2.55)

Output:

2.55

3. float value in scientific notation is displayed using print() function

>>>print(2.35E5)

Output:

235000.0

*** This is because 2.35E5 means 2.35 multiplied by 105. It will generate 235000.


3. Displaying Boolean values using print function()

Python supports two Boolean values True and False where True represents 1 and False represents 0. We can use print() function in Python shell to display Boolean values as follows:

I have used print() function to display Boolean values at >>> prompt twice as:

1. Boolean value True is displayed using print() function

>>>print(True)

Output:

True

2. Boolean value False is displayed using print() function

>>>print(False)

Output :

False



4. Displaying Complex numbers values using print() function

Python supports complex numbers that we must have studied in Mathematics in school. A complex number has two parts namely real part and imaginary part.

Example: 2+4i

In the above example 2 is real part and 4 is imaginary part. Alphabet i is used to represent imaginary part.

In Python, alphabet i is replaced by alphabet j.So above complex number will be represented in Python as 2+4j.

We can use print() function to display complex values as follows:

I have used print() function to display complex values at >>> prompt twice as:

1. Complex value 2+4j is displayed using print() function

>>>print(2+4j)

Output:

(2+4j)

2. Complex value 2.5+3.5j is displayed using print() function

>>>print(2.5+3.5j)

Output:

(2.5+3.5j)

In Python,complex values can contain floating point numbers in real part as well as complex part.



5. Displaying collections using print() function

Python basically supports four types of collections:

We can display these sequences using print() function as follows:

1. To display a List>>> print([1,2,3,4,5])
[1,2,3,4,5]
2. To display a Tuple>>> print((1,2,3,4,5))
(1,2,3,4,5)
3. To display a Dictionary
>>> print({1:’a’ ,2:’e’, 3:’i’,4:’o’, 5:’u’})
{1:’a’ ,2:’e’, 3:’i’,4:’o’, 5:’u’}
4. To display a Set
>>> print({1,2,3,4})
{1,2,3,4}

 

Lesson tags: display output in python, display value in python, output in python, print value in python, print() function, show output in python
Back to: Python Programming Tutorial