Getting started with PYTHON!!! #2.2 - Escape Sequence

Getting started with PYTHON!!! #2.2 - Escape Sequence

Hello People, In this blog we'll talk about

  • What escape sequence actually is, types of escape sequence & how to use them.

Before we start with our lesson, let us recall Blog 2.1 .

  • We leant about variables(x) which is a symbolic name used to store some value, how to assign a variable(x=5). Similarly, Identifier is a name given for some value.
    x=5
    print(x)
    
  • Keywords are pre-defined words which have pre-existent defination.
    global x=10    #global is a keyword here , this line signifies that x is a global variable.
    print(x)
    
  • Literals is the user-defined inputs given to an identifier. Eg : a=1 (1 is a numeric literal)
    n=5     #5 is numeric literal
    f=1.2     #1.2 is a floating literal
    c="a"     #a is a character literal
    s="Soham"    #Soham is a string literal
    b=True   #True is a boolean literal and True has a value of 1 whereas False has a value of 0 .
    

Now, let us start with our current topic, i.e, escape sequence. When we hear the term escape, the first thing that comes to our mind is going to a new place by exiting from an old place or getting out of something and entering a new domain by running away from previous statement . Similarly escape sequence is used to get out of a line and go to the next line or get away from a statement by a tab space.

ESCAPE SEQUENCE : An escape sequence is a set of characters that has a special meaning to the Python compiler. In the escape sequence, a character is preceded by a backslash ().

Python online compiler

TYPES OF ESCAPE SEQUENCE
  • \n (used for escaping to the nextline or newline)
    print("Hello viewer \n Welcome to my blog")
    

    O/P

    Hello viewer
    Welcome to my blog
    
  • \t (used for horizontal tab space)
    print("Do share my blog. \t Thank You")
    

    O/P

    Do share my blog.             Thank You
    
  • \v (used for vertical tab space)
    print("Do give feedback on my blog if it has been useful to you. \v Thank You")
    

    O/P

    Do give feedback on my blog if it has been useful to you.  
                                                              Thank You
    
  • \xhhh (displays the character with the respective hexadecimal value)
    print("\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64\x21")
    

    O/P

    Hello World!
    
  • \ooo (displays the character with the respective octal value)
    print("\110\145\154\154\157\40\127\157\162\154\144\41")
    

    O/P

    Hello World!
    
  • \ (is used to ignore the backslash and newline)
    print("L1 \
    Newline")
    

    O/P

    L1 Newline
    
  • \' or \" (is used to print the single or double quote)
    print("Hello \' Viewer\" ")
    

    O/P

    Hello ' Viewer"