oursolutionarchitectoursolutionarchitect
  • Python Questions and Answers
  • Python - Programming Examples
  • Python - Quick Guide
  • Python - Useful Resources
  • Python - Discussion
    • Selected Reading
    • Q&A

    Python - Escape Characters


    In Python, a string becomes a raw string if it is prefixed with "r" or "R" before the quotation symbols. Hence 'Hello' is a normal string whereas r'Hello' is a raw string.

    >>> normal="Hello"
    >>> print (normal)
    Hello
    >>> raw=r"Hello"
    >>> print (raw)
    Hello
    

    In normal circumstances, there is no difference between the two. However, when the escape character is embedded in the string, the normal string actually interprets the escape sequence, whereas the raw string doesn't process the escape character.

    >>> normal="Hello\nWorld"
    >>> print (normal)
    Hello
    World
    >>> raw=r"Hello\nWorld"
    >>> print (raw)
    Hello\nWorld
    

    In the above example, when a normal string is printed the escape character '\n' is processed to introduce a newline. However, because of the raw string operator 'r' the effect of escape character is not translated as per its meaning.

    Escape Character

    An escape character is a character followed by a backslash (\) that tells to the Interpreter that this escape character (sequence) has a special meaning.

    Example

    The newline character \n is one of the escape sequences identified by Python. Escape sequence invokes an alternative implementation character subsequence to "\". In Python, "\" is used as escape character. Following table shows list of escape sequences.

    Unless an 'r' or 'R' prefix is present, escape sequences in string and bytes literals are interpreted according to rules similar to those used by Standard C. The recognized escape sequences are −

    Escape Characters in Python

    The following table shows the different escape characters used in Python -

    Sr.No Escape Sequence & Meaning
    1

    \<newline>

    Backslash and newline ignored

    2

    \\

    Backslash (\)

    3

    \'

    Single quote (')

    4

    \"

    Double quote (")

    5

    \a

    ASCII Bell (BEL)

    6

    \b

    ASCII Backspace (BS)

    7

    \f

    ASCII Formfeed (FF)

    8

    \n

    ASCII Linefeed (LF)

    9

    \r

    ASCII Carriage Return (CR)

    10

    \t

    ASCII Horizontal Tab (TAB)

    11

    \v

    ASCII Vertical Tab (VT)

    12

    \ooo

    Character with octal value ooo

    13

    \xhh

    Character with hex value hh

    Escape Characters Example

    The following code shows the usage of escape sequences listed in the above table −

    # ignore \
    s = 'This string will not include \
    backslashes or newline characters.'
    print (s)
    
    # escape backslash
    s=s = 'The \\character is called backslash'
    print (s)
    
    # escape single quote
    s='Hello \'Python\''
    print (s)
    
    # escape double quote
    s="Hello \"Python\""
    print (s)
    
    # escape \b to generate ASCII backspace
    s='Hel\blo'
    print (s)
    
    # ASCII Bell character
    s='Hello\a'
    print (s)
    
    # newline
    s='Hello\nPython'
    print (s)
    
    # Horizontal tab
    s='Hello\tPython'
    print (s)
    
    # form feed
    s= "hello\fworld"
    print (s)
    
    # Octal notation
    s="\101"
    print(s)
    
    # Hexadecimal notation
    s="\x41"
    print (s)
    

    It will produce the following output

    This string will not include backslashes or newline characters.
    The \character is called backslash
    Hello 'Python'
    Hello "Python"
    Helo
    Hello
    Hello
    Python
    Hello Python
    hello
    world
    A
    A