Variable handling and print() function

By | April 24, 2018

Going further I want you to type the programs in your installation. I feel typing program will enhance our process of learning. Do not copy rather type and execute it in another possible combination for that particular logic/program.

Let us see basic declaration of a variable:

If you are familiar with any other programming language the declaration will be as below.

Example C:

int i=9; //defining and initializing a variable i of int type

Example Java:

boolean b=true; //defining and initializing a variable b of Boolean type.

Basically it is type of variable followed by variable name, Let us see how it works in Python

Lets break down the program

Line 1 is a comment.

Line 2 is declaration and initialization of variable foo

Line 3 printing foo value to the console.

So in Python 3 you need not to mention the type of variable in the process of declaration. You can simply declare  as


Let us understand the print() function present at line 3.



It is an inbuilt function available in Python 3 to give the string passed inside () as output to console.


Whenever you want to print something to console/or as on output. However developers will widely use it for the purpose of debugging while coding.


Let us learn with below piece of code:

  1.  Line 2,3 and 5  in code snippet gives same output.
  2. Conclusion is you can pass string either in single quotes or double quotes or in triple double quotes to print function. Do find output when you try with two doubles quotes!!
  3.  Line 4 yields a simple new blank line. print() function by default ends with a newline.
  4.  What if you don’t want a new line by default, Well we can do this with end parameter.  Try this                                       print(“Hello World”, end=’    ‘)

By default end value will be taken as \n. That’s why it will give new line for every print().

Observe first print statement ends with space and second one with a comma.

I want to print a statement along with value of some variable. Lets check how to do this:

Lets dissect code:

Line 3 – Declared a string variable and initialized it.  There is a small difference between line 4 and line 5. Line 4 we used comma to pass variable and Line 5 it is +.

Observe that both gave output but line 4 (,) gives space by default between variable and string enclosed in double quotes whereas line5 (+) gives no space.

However this + way will work only if it is a string variable. Try the same with foo in the above example.

print(“value of foo is”+foo)

Your interpreter will throw an error because it is expecting a string whereas foo is an int.

+ will behave differently with different data types. See the below snippet.

For string data type it is concatenating both the inputs(Line 4 and 5). For int it is performing arithmetic addition(Line 3). Let us how it behaves when one input is int but the another input is string type.

Hence Python + operator cannot handle when both inputs are different data types.

Will * Operator behave same as that of +:

Observe that * expects one int and another string operator. It will give the output as string with int number of times. That’s why line 2 yields Hello 3 times, line 5 yields Hello 5 times. Providing both inputs as strings will throw error like above.

Try print(“string”*5.0) . Your interpreter might be scolding you!!

Summary is + will expect both inputs as same data type where as * expects both inputs as different types.

And if you are providing multiple arguments to print statement  it will separate them by default with single space.



Hello Master Shifu

To have a specific separator we need to mention it explicitly with sep keyword.




Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.