print() function in python

Spread the love

To display output or results, Python provides the print() function. This function can be used in different formats which are discussed below.

The print() statement –

When the print function is called simply, it will throw the cursor to the next line. It means that a blank line will be displayed.

The print(“string”) statement –

When a string is passed to the print() function, the string is displayed as it is.

In [1]: print("Hello, world!")
Hello, world!

Instead of double quotes, you can also use single quotes. Both have the same meaning.

In [2]: print('Hello, World!')
Hello, World!

We can also escape sequence characters inside the print function. An escape sequence is a character that contains a special meaning. For example ‘\n’ indicates new line. ‘\t’ represents tab space.

In [3]: print('Python is awesome. \nI love python.')
Python is awesome. 
I love python.

In [4]: print('Python is awesome. \tI love python.')
Python is awesome. 	I love python.

To escape the effect of escape sequence, we should add one more ‘\’ (backslash) before the escape sequence character.

In [5]: print('Python is awesome. \\nI love python.')
Python is awesome. \nI love python.

We can use repetition operator (*) to repeat the string in the output as

In [6]: print(3 * 'python')

When we use ‘+’ operator on strings, it will join one string with another string. Hence ‘+’ is called concatenation (joining) operator when used on strings.

In [7]: print('python ' + 'is ' + 'awesome')
python is awesome

The print(variable list) statement –

We can also display the values of variables using the print() function. A list of variables can be supplied to the print() function as.

In [8]: a, b = 5, 10

In [9]: print(a)

In [10]: print(a, b)
5 10

Observe that the value in the output are separated by a space by default. To separate the output with a comma, we should use sep attribute as shown below. sep represents separator.

In [11]: print(a, b, sep=",")

In [12]: print(a, b, sep=":")

In [13]: print(a, b, sep="-----")

When several print() functions are used to display output, each print() function will display the output in a separate line as shown below.

print("I am learning python.")
print("Python is awesome.")
print("I love python.")
I am learning python.
Python is awesome.
I love python.

Each print() function throws the cursor into the next line after displaying the output. This is the reason we got the output in 3 lines. We can ask the print() function not to throw the cursor into the next line but display the output in the same line. This is done using end attribute.

print("I am learning python.", end=" ")
print("Python is awesome.", end=" ")
print("I love python.", end=" ")
I am learning python. Python is awesome. I love python.

The print( object ) statement –

We can pass objects like lists, tuples or dictionaries to the print() function to display the elements of those objects.

In [17]: lst = ["Hello", 5, 2.5]

In [18]: print(lst)
['Hello', 5, 2.5]

In [19]: a_dict = {'Max':80, 'Steve': 85, 'Mike': 90}

In [20]: print(a_dict)
{'Max': 80, 'Steve': 85, 'Mike': 90}

The print(“string”, variable list) statement –

The most common use of the print() function is to use strings along with variables inside the print() function.

In [21]: a = 2

In [22]: print(a, "is a even number.")
2 is a even number.

In [23]: print("You typed", a, "as input.")
You typed 2 as input.

Rating: 1 out of 5.

Leave a Reply