Printing | Python programmingPrinting 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.
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.
#bin/python 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'
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 tmp1.py print ('it's a python code') [email protected]:~/programming$ python tmp1.py File "tmp1.py", line 1 print ('it's a python code') ^ SyntaxError: invalid syntax [email protected]:~/programming$ nano tmp1.py [email protected]:~/programming$ cat tmp1.py print ('it\'s a python code') [email protected]:~/programming$ python tmp1.py it's a python code [email protected]:~/programming$
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$ cat tmp1.py print 4 + 5 + 3 print 4 + 5 #+ 3 print ('HacksLand')#Ethical hacker's playground [email protected]:~/programming$ python tmp1.py 12 9 HacksLand
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.
[email protected]:~/programming$ python tmp1.py I will only run in python 2.x [email protected]:~/programming$ python3 tmp1.py File "tmp1.py", 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')?