You are on page 1of 4

Print in Python

Python is known for its simplicity. So, let's tell the computer to say hello to us.

print("Hello World")

Output

Hello World

And that's it!

To print any message on screen, just type print() and that message in (' ') or (" "). Isn't
it simple?.

'print' is a function which prints something on the screen.

In Python 2, 'print' is not a function, it is a statement.

Let's see some more examples:

print("Hey! I am an upcoming coder")print("I am going to step into world of programming")

Output

Hey! I am an upcoming coder


I am going to step into world of programming

print("I am loving this. Print, print and print!")

Output

I am loving this. Print, print and print!


Let's do something more advanced.

x = 10

y = 5

print("sum of",x,"and",y,"is",x+y)

Output

sum of 10 and 5 is 15

Here, x = 10 means that we are taking a variable x and giving it a value of 10.

y = 5 → Similar to x = 10, y is variable which is assigned a value of 5.

print("sum of",x,"and",y,"is",x+y) → Take a note that "sum of", "and" and "is" are
written inside " " but x, y and x+y are not and these are seperated using commas (,).

Whatever we write inside " " or ' ' gets printed as it is and is not evaluated. So, 'Sum
of', 'and' and 'is' got printed as it is. But x, y and x+y are not written inside '
' or " ", so they are variables and the complier will use their value. That's why the
value of x got printed (i.e., 10).

Similarly, the value of y (i.e., 5) got printed and at last, the value of x+y got
calculated (i.e., 15) and got printed.

Take a note that using comma (,) also adds a space. For example, you can see a space printed
between "sum of" and 10 i.e., "sum of 10".

Now, let's get this more clear.

x = 10

print(x)

print('x')

Output

10
x

As you can see, 'x' printed x and x printed 10. This means that 'x' is simple
alphabetical x and x (without quotes) is a variable with a value of 10.

x = "Hello World"

print(x)

print('x')

Output
Hello World
x

Here also, x is a variable which stores a value of "Hello World",


so print(x) printed Hello World. Whereas, 'x' is simple x and
thus, print('x') printed x.

By practice, you will learn these things more effectively. So, make sure to solve questions
from the Practice Section.

Take a notes that there is no difference between ' ' (single quotes) and " " (double quotes)
in Python which are different in many other languages.

Let's Do Some Error Analysis


This part is only to teach you how to know the error if you are getting one.
What if we type this :

x = 10

prin(x)

Here, we are trying to teach you, how to fix an error, if you got one.

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

NameError: name 'prin' is not defined

This is telling us that there is an error is in line 1

NameError: name 'prin' is not defined → 'prin' is nothing for our program because it is not
defined anywhere in our program. That's why it is giving us an error.
Go back to your code and then to that line (prin(x)) and fix it.

In future, when writing long codes, make sure that you run your code from time to time while
writing instead of completing your whole code and running at last. This will make debugging your
code a lot easier.

Without practice, your knowledge is poison.

- Chanakya

You might also like