Escape Characters in Python

In Python, escape characters are special characters used to insert characters that are illegal or difficult to type directly in a string. These characters are preceded by a backslash (\) .

Escape characters help us include special characters in strings that would otherwise cause errors or be interpreted differently. For example, inserting a new line, tab space, quote inside a quote, etc.

Escape Characters:

Escape Sequence Description
\' Inserts a single quote
\" Inserts a double quote
\\ Inserts a backslash
\n Inserts a new line
\t Inserts a tab space
\b Inserts a backspace (moves cursor one step back)
\r Inserts a carriage return (moves cursor to beginning of line)
\f Inserts a form feed (new page character, rarely visible)
\a System alert / bell sound (may not work on all systems)
\v Inserts a vertical tab

Example:

print("1. Double quote: \"Hello\"")
print('2. Single quote: It\'s Python')
print("3. New Line:\nThis is on a new line")
print("4. Tab Space:\tThis is tabbed")
print("5. Backslash: \\")
print("6. Backspace: ABC\bD")
print("7. Carriage Return: Hello\rStart")
print("8. Form Feed: Line1\fLine2")
print("9. Vertical Tab: Text1\vText2")
print("10. Null Character: End\0Start")

1. Double quote: "Hello"

2. Single quote: It's Python

3. New Line:
This is on a new line

4. Tab Space: This is tabbed

5. Backslash: \

6. Backspace: ABD

7. Carriage Return: Starto

8. Form Feed: Line1 Line2

9. Vertical Tab: Text1 Text2

10. Null Character: EndStart

Explanation:

  • \" → Inserts double quotes inside double-quoted string.
  • \' → Inserts a single quote inside single-quoted string.
  • \n → Inserts a newline character and moves the text after it to a new line.
  • \t → Adds a horizontal tab (similar to pressing the Tab key).
  • \\ → Prints a single backslash.
  • \bBackspace: Deletes the character just before it.
    For example: "ABC\bD" becomes "ABD" because \b deletes 'C'.
  • \rCarriage return: Moves the cursor to the beginning of the current line **without a newline**, and the characters after \r start overwriting from the beginning.
    "Hello\rStart" becomes "Starto":
    – "Hello" is written first, then \r returns cursor to start.
    – "Start" overwrites "Hello" starting from the beginning → **"Start" + "o"** (from Hello).
  • \fForm feed: Inserts a form feed character which is used in printers to move to the next page.
    In most terminals, it may appear as a small symbol or space: "Line1\fLine2" appears like a space or unreadable character between lines.
  • \vVertical tab: Inserts vertical space (like a tab, but vertical direction).
    Example: "Text1\vText2" – output may look like line break or special spacing depending on the environment.
  • \0Null character: Indicates end of string in C/C++, but in Python it is treated as just a character with no visible effect.
    "End\0Start" prints as: "EndStart" (with invisible separator).

Summary:

  • Escape characters start with a backslash (\) and allow special formatting inside strings.
  • They help include characters like newlines, tabs, quotes, and file paths in a proper way.
  • Common ones include \n, \t, \\, \', \", etc.
  • Some escape characters like \r and \b behave differently depending on the platform and terminal.
  • Some escape characters (like \a, \v, \f, \0) may not show visible output in all environments but still exist in the string.

Welcome to ShikshaSanchar!

ShikshaSanchar is a simple and helpful learning platform made for students who feel stressed by exams, assignments, or confusing topics. Here, you can study with clarity and confidence.

Here, learning is made simple. Notes are written in easy English, filled with clear theory, code examples, outputs, and real-life explanations — designed especially for students like you who want to understand, not just memorize.

Whether you’re from school, college, or someone learning out of curiosity — this site is for you. We’re here to help you in your exams, daily studies, and even to build a strong base for your future.

Each note on this platform is carefully prepared to suit all levels — beginner to advanced. You’ll find topics explained step by step, just like a good teacher would do in class. And the best part? You can study at your pace, anytime, anywhere.

Happy Learning! – Team ShikshaSanchar