Escape Sequences in Python

Escape Sequences in Python

Comments and Escape Sequences

·

2 min read

Hey There! You can revise some useful Escape Sequences. Comments are also included in this blog. Have a Great Day!!!

Comments

Comments are mainly used in code to markdown functionality. Comments are something that compilers ignore or Compilers do not compile Comments, If you do not know what is Compiler: Compiler reads code and fetches result which is shown in Terminal

There are

  1. Single Line Comments
  2. Multi Line Comments

# is used for Single Line Comment and ''' ''' is used for Multi Line Comments, """ """ is also used for Multi Line Comments.

#Single Line Comment
'''Multi
Line
Comment'''
"""Multi
Line
Comment
"""

Escape Sequences

In Python when we print something by default it ends with new line.

print("Hello World!")

terminal.png Path Working Directory is printed in next line(~ $) cause string ends with new line by default, We can change that!

print("Hello World!", end="")

terminal.png Some Examples:

print("Hello ", end="World!")

Terminal:

terminal.png Let's try the default one

print("Hello World!", end="\n")

terminal.png \n Creates new line Let's Discuss more about escape sequences :))

Single / Double Quotation Mark (\', \")

Printing Single Quotation Mark:

print("Hello\'World!")

Terminal:

Hello'World!

Double Quotation Mark:

print("Hello\"World!")

Terminal:

Hello"World!

New Line (\n)

print("Hello\nWorld!")

Terminal:

Hello
World!

Tab (\t)

print("Hello\tWorld!")

Terminal:

Hello    World!

Backslash (\\)

print("Hello\\World!")

Terminal:

Hello\World!

Form Feed (\f)

print("Hello\fWorld!")

Terminal:

Hello
       World!

Carriage Return (\r)

to Chop off Text before

print("Hello\rWorld!")

Terminal:

World!

Backspace (\b)

print("Hello \bWorld!")

Terminal:

HelloWorld!

These were some useful sequences but there are bunch of sequences, you can explore them later on.

Thank you for reading;-; you can Google and explore other sequences
also Check: W3Schools Escape Chracters

terminal.png