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 section first and know how to write and compile python codes. If not, then go through introduction the section first. )

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)
Let's see some more examples:-

In Python 3, the 'print' syntax is 'print ()'. So, if you are using Python 3 then print "Hello World" should be written as print ("Hello World")

For Python 3:

print ("Hello World!")
Output
Hello World!
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

For Python 3:

x = 10
y = 5
print ("sum of",x,"and",y,"is",x+y)
Output
sum of 10 and 5 is 15

Here, x = 10

It means that we are taking a variable x and giving it a value of 10.

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

print "sum of",x,"and",y,"is",x+y
Let's learn about this line of code.
Whatever we write in (" ") or (' ') gets printed as it is and is not evaluated. So, 'Sum of' gets printed and then the value of 'x' gets printed (i.e. 10), then 'and' is printed as it is. Then value of y (i.e. 5) and at last x+y is calculated and printed. (i.e. 15)

Take care of how commas (,) are used in the above code. You can also write the same code in different ways, which you will learn soon.

Now, let's get this more clear.

x = 10
print x
print 'x'
Output
10
x

As you have seen, 'x' is a character and x is a variable. This means that 'x' is simple (x) and x (without quotes) is 10.

x = "Hello World"
print x
print 'x'
Output
Hello World
x

By practice, you will learn these things more effectively.

print in python

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.

 File "<stdin>", line 1
    prin x
        ^
SyntaxError: invalid syntax


This is telling us that there is an error is in line 1 and '^' is pointing where it is.
Syntax error : invalid syntax
prin is nothing for our program. That's why it is giving us syntax error (as we know correct syntax would be print).
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 easier if you have made some errors.

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


Download Our App.
BlogsDope App
Get it on Google Play
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.