Print Statement, Escape Sequence and comments in Python Programming.


Print Statements:

Print statement is the most important and basic function of python programming. It prints the result on the screen or any out put device. e.g. from the below mentioned print statement/coding "Hello Everyone" is the result which will be printed on output device/screen.


Print Syntax

print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.

Optional keyword arguments:

file:  a file-like object (stream); defaults to the current sys.stdout.
sep:   string inserted between values, default a space.
end:   string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.


Print a single value

print("Hello P-World")

 

Run Result will be

Hello P-World

 

Printing multi values

print("Hello P-World","This is my first blogpost", "I hope everyone enjoying good health")

 

Run Result will be

Hello P-World This is my first blogpost I hope everyone enjoying good health

 

Print using optional keyword "Sep"

print("Hello P-World","Welcome to Python excercise","On Digital Drive" Sep="---")

 

Run Result will be

Hello P-World---Welcome to Python excercise---On Digital Drive

 

Print using optional keyword "end"

print("Hello P-World","Welcome to Python excercise", end=" ")

print("On Digital Drive")

 

Run Result will be

Hello P-World Welcome to Python excercise on Digital Drive

 

Printing after defining value

 

A=("Hello P-World","This is my first blogpost", "I hope everyone enjoying good health")

print(A)

 

Run result will be

Hello P-World This is my first blogpost I hope everyone enjoying good health

 

Escape Sequences:

It is the sequence of special character that does not represent itself but followed by a character, it translate it into a sequence which the programmer want to insert into a program. e.g. backslash “\” is special/escape character and by putting another word it changed the meaning and sequence of statement. These are also used to print the non-printing characters like \ ‘, “,#, etc.

 

“\n” for newline

“\t”  for tab

"\v" for vertical tab

"\b" for Backspace

“\ \” for backslash

“\’”  for quote

“\”” for inverted comma

 

Printing by using new line and tab escape sequence

print (“Hello P-world \n This is \t Digital drive”)

 

Run Result will be

Hello P-World

This is        Digital drive

 

Comments:

Comments are those statements which make the coding understandable in any complex coding. It is mostly used to remember the background of the specific codes. It is also commonly used by the programmers when a number of programmers are working on one project to instruct the programmers that why the specific code is being used here and what the impact and result it will deliver to this project. Following are the examples of comments.

One liner comments

#By typing hash you can insert comments for memorizing the importance of code. Either you can put your comments before the code line or after the code end.

Print ("Hello P-World") 

Print ("This is a test project") #You can also put comments after the code end but you cannot put your comments before the code starting.

Run Result will be

Hello P-World

This is a test project

 

Multi lines comments

"""You can start 

multi line comments 

with triple inverted commas or triple single qoutes (''')

and close the comments with triple inverted commas or triple single qoutes"""

 

print ("Hello Everyone")

 

Run Result will be

Hello Everyone


Comments