Aug 12, 2019

Printing | Python programming

Printing is an absolutely basic part of a programming language. We learned how we can print a string with PHP and C. So in this tutorial we are going to talk about python printing. Hear I explain you both python 2 and 3. Let's see following program.
print ('welcome to HacksLand')
print ("this code's syntax is OK. But why?")
print ('guys, it/'s my first python code')
#this is the way we make a comment in python

I am also a comment that wrap to many lines . But this commenting way is not official

print ('I am ok in both python 2.x and 3.x')
print 'I will only run in python 2.x'
  First thing to understand is syntax of printing something. print ('string goes hear') We can use print() function to do this. You want to put the string as the argument for that function. (I'll explain more about functions in a later tutorial). What if you include a quot in your string? It will disturb Python's syntax.
[email protected]:~/programming$ cat
print ('it's a python code')

[email protected]:~/programming$ python
  File "", line 1
    print ('it's a python code')
SyntaxError: invalid syntax
[email protected]:~/programming$ nano
[email protected]:~/programming$ cat
print ('it\'s a python code')

[email protected]:~/programming$ python
it's a python code
[email protected]:~/programming$
In next line I opened a nano editor and corrected the error. After I ran it again and its working fine. So what the magic happened hear when we used a backward slash? Its a escape symbol. That mean when python interpreter see a backward slash it'll ignore next character. Actually this is not a ignoring. It think next character is a specific one and should not printed normally. Also there are some other escape characters . We use those sometimes in our programs. \n - This one is used to indicate a line break. If we use this between a string it will break and go to another line. \t - This acts as a tab space. \\ - We can print a backward slash with this. Why it happen?? Think.! Now you need to do experiments with those. Do it yourself. Next we have another problem. This string also uses a quote inside it. But it's not produce an error. print ("this code's syntax is OK. But why?") This is because hear we have used different quotation marks for indicate the string. If we want to make a comment we use '#' symbol. In a previous tutorial I have explained you why we use comments in our programs. Also we can use triple quotes for a multi line code. This is very helpful when we want to disable a code block for debugging purpose.
[email protected]:~/programming$ cat
print 4 + 5 + 3
print 4 + 5 #+ 3
print ('HacksLand')#Ethical hacker's playground

[email protected]:~/programming$ python
Finally I have listed another way to print a string. Hear we don't use parentheses. But this way is not correct in python 3.x version.
[email protected]:~/programming$ python
I will only run in python 2.x
[email protected]:~/programming$ python3
  File "", line 1
    print 'I will only run in python 2.x'
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('I will only run in python 2.x')?
So it's a good programming practice to always use first method for python printing. Because it's important to learn both versions of python.
Jun 21
Protostar Stack1 Tutorial

In previous tutorial I completely explained how to exploit protostar stack 0 vulnerable program. In....

Apr 09
C programming pointers explained

Pointer is a important concept in C programming. In this document we are going to learn about....

Aug 12
Variables | Python programming

You know that variable is a memory space ant it contains a value. When we talk in low level that's....

Replying to 's comment Cancel reply
Thilan Danushka Dissanayaka

Thilan Dissanayaka

Hi, I'm Thilan from Srilanka. An undergraduate Engineering student of University of Ruhuna. I love to explorer things about CS, Hacking, Reverse engineering etc.