• Category
  • >Python Programming

Python To Represent Output

  • Priyanshu Gupta
  • Jan 30, 2021
Python To Represent Output title banner

In our previous blog on the First Step towards Python, we have set up our Lab environment for Python and got an understanding of the Power of Interpreted Language using Python. 

 

Let’s move ahead in the journey of Python and deep dive with the concepts...

 

 

Why Python & Why not Python:

 

Consider if you have to write your code like this 00001111, 11100101, etc. 

 

Is it possible to write, understand & debug a code easily in this way? Is it an efficient & time-saving way of writing code?

 

Programming in any language is just understandable to humans because machines can’t understand it in human-readable form. Machines first convert the whole program in machine-readable form (0 & 1 Binary Codes).  

 

Definitely, Binary code is the fastest way of executing any program, because here the consumption of time between human-readable to machine-readable is separated from the process.

 

Python is an efficient medium of writing, understanding & debugging code for various operations. But when a requirement of speed comes into play Python is not for you. In the domain of development of Drivers & Graphical Engine Python is not efficient till now.   

 

You can also sneak a peek at our blog on 20 Python Interview Questions in Data Science

 

Python is a High-Level Programming Language that is close to Humans while Binary is a Low-Level Programming Language that is close to Machines.


 

Example:

 

Human readable form of my name - Priyanshu Gupta

 

Machine-readable form of my name - 

10100001110010110100111110011100001110111011100111101000111010110000010001111110101111000011101001100001

 

Be with us we will learn it in this series of python Blogs after some time or try to find binary code of your name. Let’s move ahead with some hands-on in Python…


 

Hands-On With Python:

 

  • How to represent outputs in Python:

 

  1. print() Function:

 

This function is used to print the desired output of your code on Screen.


 

print("Hey,")

print()

print("How's The JOSH?")



Hey,



How's The JOSH?

 

Here, print() function without any argument is also valid in python. When we do not pass any argument to print() function, it outputs as a blank line.


 

print("Welcome to the Python")

Welcome to the Python



print("Welcome to","the Python")

Welcome to the Python



print("Welcome", "to", "the","Python")

Welcome to the Python



print(Welcome to the Python)

  File "<stdin>", line 1

    print(Welcome to the Python)

                   ^

SyntaxError: invalid syntax

As you can see we printed the same output in different manners. Here you need to observe that print() function can contain multiple arguments.  To separate multiple arguments we use a separator comma (,).

 

SyntaxError: invalid syntax

 

(This error means python doesn't allow this type of language representation. It’s grammatically not allowed.)


 


print("a")

a



print(a)

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

NameError: name 'a' is not defined

 

NameError: name 'a' is not defined

 

(This error means grammar is correct but you can not use it like this. First You have to define it then you can use it.)
 


print("5")

5



print(5)

5


Here, we printed words, numbers, letters but the behavior towards numbers is different than words & letters. To print words & letters it’s compulsory to close them in inverted commas  (“ ...”) while to print a number it’s not compulsory. But definitely the behaviour of a number inside inverted commas is completely changed. We will verify these later. 



 

  1. Escape Characters ( \ ) :

 

These Escape characters are predefined in python and only allowed inside strings.


 print("\")

  File "<stdin>", line 1

    print("\")

             ^

SyntaxError: EOL while scanning string literal



print("\\")

\

When you try to print(“\”) it then here you get a SyntaxError because  backslash needs a character to define its meaning for the string.

 

While you try to print(“\\”) it then there is no error, but it prints only a single backslash. It means that every character after backslash has some meaning pre defined in Python. Here we will learn some important escape characters:

 

Newline Character  (\n): 

 

This Newline Character (\n) is used to change the line or break the line in two different lines. 


 

print("Hey,\nHow's The JOSH?")

Hey,

How's The JOSH?



print("Hey,"\n"How's The JOSH?")

  File "<stdin>", line 1

    print("Hey,"\n"How's The JOSH?")

                                   ^

SyntaxError: unexpected character after line continuation character


 

Because escape characters are predefined only inside strings, otherwise it gives SyntaxError.

 

Tab Character (\t): 

 

This Tab Character (\t) is used to apply extra space inside in strings.

 


print(".......\tPython\t.......")

....... Python  .......



print(".......\t\t\tPython.......")

.......                 Python.......

 

You can use multiple escape characters inside strings.

 

Backspace Character (\b):

 

This backspace character (\b) is used to apply a backspace in string.

 


print(" Hii\b")

 Hii



print(" Hii\b\b")

 Hii



print(" \bHii")

Hii

 

As you can clearly see that backspace meaning of your Keyboard & Python have little bit difference. When you use backspace using keyboard it erases characters & space both, while when you use backspace in python it only erases space & shift string.

 

Try to learn more about Escape Characters by doing experiments with different combinations of backslash & characters yourself like this.

 

  1. end Keyword:

 

This end Keyword is used to provide the last escape character to the string or it decides the movement of the next string of print() function.

 


print("Hey,")

print("How's The JOSH?")

print()

print("Hey,",end="")

print("How's The JOSH?")



Hey,

How's The JOSH?



Hey,How's The JOSH?

 

Here, you can clearly observe that a string by default contains a Newline Character in the last end of the string to jump output to the next new line.

 


print("Hey,",end="\n")

print("How's The JOSH?")

print()

print("Hey,",end="\t")

print("How's The JOSH?")



Hey,

How's The JOSH?



Hey, How's The JOSH?

 

You can also provide escape characters of your own choice to behave accordingly.

 

  1. sep Keyword:

 

This sep Keyword (Separator) is used to provide a separator between arguments output.


print("North","West","South","East")

print("North","West","South","East",sep="-")



North West South East

North-West-South-East

 

Here, you can clearly observe that by default, the separator between output of arguments is a Space.  We replace this space with our own choice character using the sep keyword.

 


 

Application of Concepts:

 

We have learnt a lot about representation of outputs. Let’s apply these concepts to create a Terminal User Interface (TUI) representation using Python.

 


 

print("...............................Power of Python.......................................")

print("Welcome to Power Of Python")

print("Press 1: To run Program1","Press 2: To run Program2")

print("Press 3: To run Program3  Press 4: To run Program4")

print(" Press 5: To run Program5")

print("Press 6","To run Program6")

print("Press 7")

print("To run Program7")

 

Welcome to Power Of Python

Press 1: To run Program1 Press 2: To run Program2

Press 3: To run Program3  Press 4: To run Program4

Press 5: To run Program5

Press 6 To run Program6

Press 7

To run Program7

 

We write our Terminal User Interface like this. But if you see this representation is very unclear to the user or client of your TUI program. So, to make it more clear let’s apply the concepts which we have understood very well above.

 

print("............................... Power of Python .......................................")

print("\t\t\tWelcome to Power Of Python")

print("Press 1: To run Program1\n","\bPress 2: To run Program2")

print("Press 3: To run Program3  \nPress 4: To run Program4")

print(" \bPress 5: To run Program5")

print("Press 6"," To run Program6",sep=":")

print("Press 7:",end="")

print(" To run Program7")




............................... Power of Python .......................................



Welcome to Power Of Python

Press 1: To run Program1

Press 2: To run Program2

Press 3: To run Program3  

Press 4: To run Program4

Press 5: To run Program5

Press 6: To run Program6

Press 7: To run Program7

 

Now you can see the Terminal User Interface for your Client is very clear. So, all the above operations used to represent a very clear structure of your Program. 

Here this program is just a representation but we will operate some of our program from this type of Terminal User Interface and will create our own Tools. 


 

 

Conclusion

 

Python is more close to Humans than Binary which is closer to Machines. To understand the structure & functionality we use Python rather than Binary otherwise debugging in the world of Computer Science becomes more complex. Because the infrastructure of an Organization contains millions of Lines of Codes. So it is too hard to debug it. High-Level Programming languages make it simple & understandable to humans.

 

Cyber Security & Networking World contains a maximum of the programs with their Terminal User Interfaces. Because Terminal User Interface (TUI) provides more flexibility to operations rather than Graphical User Interface (GUI). Definitely, TUI is faster than a GUI Program because GUI consumes GPU, CPU & RAM to process a lot. GUI has the advantages of user experience, but it should be evident.  

Latest Comments