Print Function in Python

To proceed with this post you need to install Anaconda. Please read this post.
http://blogs.hypatiasoftwaresolutions.in/python-machine-learning-installation/
The first difference is in the name itself. print is now a function in Python and not a statement. This means we will have to write print(x) everytime and not just print x.

The simplest way of printing is to write:
print (“Have a crackerful Deepawali”)
print in python

print can handle multiple items like this: print(x1,x2,x3)
To print variables we would write something like:
s1=”A=”
a=10
print (s1,a)
print in python

Every print statement is ended by a newline “\n”
Thus writing three print statements would print them all in different lines.

print(“one”)
print(“two”)

print(“three”)
print in python

To override this default behavior we need to define the the end variable.

print(“one”,end=””)
print(“two”,end=”:”)
print(“three”)
print(“four”)
The output should be self explanatory.
print in python

By default print elements are separated by spaces. This can be changed by defining the sep variable.
print(1,2,3,4)
print in python

print(1,2,3,4,sep=”,”)
This will print with separators being the comma.
print in python

Then we can have printing using format specifiers like printf in C.

Format Strings
1. %d stands for signed integer, so does %i
print(“%4d”% 10)
print(“%4d”% 110)
print in python

Next, we will learn the format function.

Leave a Reply