Close
Close

Python Print


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

print("Hello World")
Output
Hello World

And that's it!

To print any message on screen, just type print() and that message within single quotes ' ' or double quotes " " inside print(). Isn't it simple?

print() is a function which prints something on screen. (You will learn about functions in later sections)

In the above example, we printed the message by enclosing it within double quotes " " i.e., "Hello World". Let’s print the same message by enclosing it within single quotes ' '.

print('Hello World')
Output
Hello World

We got the same output on printing "Hello World" and 'Hello World'.

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!
x = 10
y = 5
print("sum of", x, "and", y, "is", x+y)
Output
sum of 10 and 5 is 15

You might have guessed what is happening here, yet let me explain a bit.

x = 10 → We are taking a variable x and giving it a value of 10. Yeah, similar to what we do in Maths.

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

print("sum of",x,"and",y,"is",x+y) → We already know that the print() function prints something for us but what's inside it is interesting. Let’s have a look at it.

Take a note that "sum of", "and" and "is" are written inside " " (we can also use ' ') but x, y and x+y are not. All these six arguments are separated 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 " ", as they are variables and the compiler will print their values.

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

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 a 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.

printing string and variable in Python

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

Note that there is no difference between ' ' (single quotes) and " " (double quotes) in Python.

Let's have some fun


Try the following code.

print("a" + "b")
Output
ab

Let’s have a look at another example.

print('Fun'*3)
Output
FunFunFun

The message to be printed is equivalent to "Fun"+"Fun"+"Fun".

This is the Pythonic way to get your things done. These types of features of Python can be of good use for you in the future.

Python repeation operator

Let's Do Some Error Analysis


Do you know what a harsh reality is? The more you write code the more errors you get.

Do you know what makes a programmer a ‘great programmer’? There are many things but one of the most critical ones is the ability to hunt down bugs/errors.

So, let’s start with the first step of analyzing an error.

What if we type this :

x = 10
prin(x)

We have learned about print but what is prin? Of course, not something our Python knows and thus running this code will display the following error.

Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
NameError: name 'prin' is not defined

Let’s analyze this error and extract some information which is useful for us to fix this.

This is telling us that there is an error 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.

NameError is a type of error. Python has few pre-defined error types. You can also make your own. We will look at it in a later chapter.

Go back to your code and then to that line (prin(x)) and fix it.

In future, when writing long code, 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 printing something on the screen. We also printed values of variables. So, let's move to the next chapter and learn what more we can do in Python.

It is 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.

To learn from simple videos, you can always look at our Python video course on CodesDope Pro. It has over 500 practice questions and over 20 projects.
Without practice, your knowledge is poison.
- Chanakya


Ask Yours
Post Yours
Doubt? Ask question