Loading...
Loading...
00:00:00

What Is Escpae Sequences In Python?

Escape sequences characters are used to escape the some special characters from the string. we cannot insert the double quotes inside double quotes or single quotes inside single quotes . Escape sequences characters are also used to formatting the string . Each escape sequences has special meaning . here is the list of escape characters -

Escape Sequences Meaning
\a Alarm or Beep
\b Backspace
\f Form Feed
\n New Line
\r Carriage Return
\t Tab (Horizontal)
\v Vertical Tab
\\ Backslash
\' Single Quote
\" Double Quote
\? Question Mark
\nnn octal number
\xhh hexadecimal number

In Python 3, you can use escape sequences and special characters to represent special characters in strings. An escape sequence is a sequence of characters that represents a single character.

Here are some common escape sequences in Python:

\\ : Backslash (\)
\' : Single quote (')
\" : Double quote (")
\a : ASCII Bell (BEL)
\b : ASCII Backspace (BS)
\f : ASCII Formfeed (FF)
\n : ASCII Linefeed (LF)
\r : ASCII Carriage Return (CR)
\t : ASCII Horizontal Tab (TAB)
\v : ASCII Vertical Tab (VT)
\ooo : ASCII character with octal value ooo
\xhh : ASCII character with hexadecimal value hh

Here are the some examples of escape sequence characters:

print("Welcome to \"python\" world")  # double quotes in double quotes
print("Welcome to \npython world")  # new line 
print("Welcome to \tpython world")  # tab (four spaces between words) 
print("\\python\\") # Backslash in string

In this example, the \n escape sequence is used to represent a new line, the \t escape sequence is used to represent a tab, and the \ character is used to escape other special characters, such as quotes.