logo codesdope

Print in Python


Python is known for its simplicity. So, let's tell the computer to say hello to us. (Assuming that you have gone through the Introduction chapter first and know how to write and compile Python codes. If not, then first go through the Introduction chapter.)

  • Python 2
  • Python 3
print "Hello World"
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. (You will learn about functions in detail in later sections)

In Python 2, 'print' is not a function, it is a statement. You will understand about functions in later chapters.

Let's see some more examples:

  • Python 2
  • Python 3
print "Hey! I am an upcoming coder"
print "I am going to step into world of programming"
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
  • Python 2
  • Python 3
print "I am loving this. Print, print and print!"
print("I am loving this. Print, print and print!")
Output
I am loving this. Print, print and print!

Let's do something more advanced.

  • Python 2
  • Python 3
x = 10
y = 5
print "sum of",x,"and",y,"is",x+y
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.

  • Python 2
  • Python 3
x = 10
print x
print 'x'
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.

  • Python 2
  • Python 3
x = "Hello World"
print x
print 'x'
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.

print in python
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.

In this chapter, we mainly learned about the printing something on the screen. We also printed values of variables. So, let's move to the next chapter and learn more things to do in Python.

It is very necessary to solve questions and practice. As you progress in this course, it will be very difficult to understand concepts if you are not practicing. So, it is highly recommended that you go to the practice section regularly.

Without practice, your knowledge is poison.
- Chanakya


Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.