Skip to content

Python Escape String

Escape characters in Python are special characters that don’t get printed as-is but instead signal some specific action. They allow you to include special characters in strings or control the formatting of text. In this comprehensive guide, we’ll explore the world of escape characters in Python and how they can be used to enhance text manipulation.

diagram what the backslash does, and how to turn it off mermaid
A backslash in a normal string literal is an instruction to the parser, not a character -- so the text you typed and the string you get are different lengths. A raw string switches that off, which is why regex patterns and Windows paths are written that way. The one thing a raw string still cannot do is end with a backslash.

Escape characters are denoted by a backslash (\) followed by a specific character or sequence. The backslash tells Python that the character following it has a special meaning.

SequenceEscape CharacterEscape Character NameDescription
1\nNewlineInserts a new line
2\tTabInserts a tab
3\rCarriage ReturnInserts a carriage return
4\bBackspaceInserts a backspace
5\fForm FeedInserts a form feed
6\vVertical TabInserts a vertical tab
7\\BackslashInserts a backslash
8\'Single QuoteInserts a single quote
9\"Double QuoteInserts a double quote
10\aBellInserts a bell
11\0Null ByteInserts a null byte
12\N{name}Unicode CharacterInserts a Unicode character
13\xhhHexadecimal CharacterInserts a hexadecimal character
14\uhhhhUnicode 16-bit Hexadecimal CharacterInserts a Unicode 16-bit hexadecimal character
15\UhhhhhhhhUnicode 32-bit Hexadecimal CharacterInserts a Unicode 32-bit hexadecimal character
16\oooOctal CharacterInserts an octal character

The newline escape character (\n) inserts a new line into a string. It’s useful for formatting text and creating line breaks. For example, you can use it to print a string on multiple lines:

strings.py
# print string on multiple lines
print('This is a string\non multiple lines')

Output

command
C:\Users\Your Name> python strings.py
This is a string
on multiple lines

The tab escape character (\t) inserts a tab into a string. It’s useful for formatting text and creating tabbed sections. For example, you can use it to print a string with a tab:

strings.py
# print string with tab
print('This is a string\twith a tab')

Output

command
C:\Users\Your Name> python strings.py
This is a string    with a tab

The carriage return escape character (\r) inserts a carriage return into a string. It’s useful for formatting text and creating line breaks. For example, you can use it to print a string on multiple lines:

strings.py
# print string on multiple lines
print('This is a string\ron multiple lines')

Output

command
C:\Users\Your Name> python strings.py
This is a string
on multiple lines

The backspace escape character (\b) inserts a backspace into a string. It’s useful for formatting text and creating tabbed sections. For example, you can use it to print a string with a backspace:

strings.py
# print string with backspace
print('This is a string\bwith a backspace')

Output

command
C:\Users\Your Name> python strings.py
This is a stringwith a backspace

The form feed escape character (\f) inserts a form feed into a string. It’s useful for formatting text and creating line breaks. For example, you can use it to print a string on multiple lines:

strings.py
# print string on multiple lines
print('This is a string\fon multiple lines')

Output

command
C:\Users\Your Name> python strings.py
This is a string
on multiple lines

The vertical tab escape character (\v) inserts a vertical tab into a string. It’s useful for formatting text and creating tabbed sections. For example, you can use it to print a string with a vertical tab:

strings.py
# print string with vertical tab
print('This is a string\vwith a vertical tab')

Output

command
C:\Users\Your Name> python strings.py
This is a string
with a vertical tab

The backslash escape character (\\) inserts a backslash into a string. It’s useful for formatting text and creating tabbed sections. For example, you can use it to print a string with a backslash:

strings.py
# print string with backslash
print('This is a string\\with a backslash')

Output

command
C:\Users\Your Name> python strings.py
This is a string\with a backslash

The single quote escape character (\') inserts a single quote into a string. It’s useful for formatting text and creating tabbed sections. For example, you can use it to print a string with a single quote:

strings.py
# print string with single quote
print('This is a string\'with a single quote')

Output

command
C:\Users\Your Name> python strings.py
This is a string'with a single quote

The double quote escape character (\") inserts a double quote into a string. It’s useful for formatting text and creating tabbed sections. For example, you can use it to print a string with a double quote:

strings.py
# print string with double quote
print('This is a string\"with a double quote')

Output

command
C:\Users\Your Name> python strings.py
This is a string"with a double quote

The bell escape character (\a) inserts a bell into a string. It’s useful for formatting text and creating tabbed sections. For example, you can use it to print a string with a bell:

strings.py
# print string with bell
print('This is a string\awith a bell')

Output

command
C:\Users\Your Name> python strings.py
This is a stringwith a bell 

The null byte escape character (\0) inserts a null byte into a string. It’s useful for formatting text and creating tabbed sections. For example, you can use it to print a string with a null byte:

strings.py
# print string with null byte
print('This is a string\0with a null byte')

Output

command
C:\Users\Your Name> python strings.py
This is a stringwith a null byte

The Unicode character escape character (\N{name}) inserts a Unicode character into a string. It’s useful for formatting text and creating tabbed sections. For example, you can use it to print a string with a Unicode character:

strings.py
# print string with Unicode character
print('This is a string\N{COPYRIGHT SIGN}with a Unicode character')

Output

command
C:\Users\Your Name> python strings.py
This is a string©with a Unicode character

The hexadecimal character escape character (\xhh) inserts a hexadecimal character into a string. It’s useful for formatting text and creating tabbed sections. For example, you can use it to print a string with a hexadecimal character:

strings.py
# print string with hexadecimal character
print('This is a string\x24with a hexadecimal character')

Output

command
C:\Users\Your Name> python strings.py
This is a string$with a hexadecimal character

Unicode 16-bit Hexadecimal Character Escape Character

Section titled “Unicode 16-bit Hexadecimal Character Escape Character”

The Unicode 16-bit hexadecimal character escape character (\uhhhh) inserts a Unicode 16-bit hexadecimal character into a string. It’s useful for formatting text and creating tabbed sections. For example, you can use it to print a string with a Unicode 16-bit hexadecimal character:

strings.py
# print string with Unicode 16-bit hexadecimal character
print('This is a string\u0024with a Unicode 16-bit hexadecimal character')

Output

command
C:\Users\Your Name> python strings.py
This is a string$with a Unicode 16-bit hexadecimal character

Unicode 32-bit Hexadecimal Character Escape Character

Section titled “Unicode 32-bit Hexadecimal Character Escape Character”

The Unicode 32-bit hexadecimal character escape character (\Uhhhhhhhh) inserts a Unicode 32-bit hexadecimal character into a string. It’s useful for formatting text and creating tabbed sections. For example, you can use it to print a string with a Unicode 32-bit hexadecimal character:

strings.py
# print string with Unicode 32-bit hexadecimal character
print('This is a string\U00000024with a Unicode 32-bit hexadecimal character')

Output

command
C:\Users\Your Name> python strings.py
This is a string$with a Unicode 32-bit hexadecimal character

The octal character escape character (\ooo) inserts an octal character into a string. It’s useful for formatting text and creating tabbed sections. For example, you can use it to print a string with an octal character:

strings.py
# print string with octal character
print('This is a string\044with an octal character')

Output

command
C:\Users\Your Name> python strings.py
This is a string$with an octal character

Escape characters in Python are powerful tools for manipulating text, controlling formatting, and handling special characters. Understanding how to use escape sequences enhances your ability to work with strings and create well-formatted, readable code.

As you continue your Python journey, experiment with different escape characters, explore their applications, and incorporate them into your projects. Whether you’re working with strings, handling special characters, or managing formatting, escape characters are valuable assets in your programming toolbox.

For more tips, tricks, and practical examples, check out our tutorials on Python Central Hub!

Section titled “For more tips, tricks, and practical examples, check out our tutorials on Python Central Hub!”
sketch The backslash is an instruction, until you turn it off p5.js
In an ordinary literal the parser consumes the backslash and produces one character, so the text you typed and the string you got are different lengths. A raw string skips that step entirely. Watch the character count change as the same source text is read both ways.
pch.quizTag pch.quizDefaultTitle
  1. What is `len('a b')` and what is `len(r'a b')`?

    pch.quizShowAnswer

    C — 3 and 4 — Verified. In the ordinary literal the parser turns backslash-n into one newline character; the raw string keeps both characters.

  2. Why are regular expressions usually written as raw strings?

    pch.quizShowAnswer

    B — So the backslash reaches the regex engine instead of being consumed by Python — Without a raw string you would have to double every backslash — once for Python's parser and once for the regex engine — which is where the classic four-backslash confusion comes from.

  3. What does `r'C:'` do?

    pch.quizShowAnswer

    B — SyntaxError — a raw string cannot end with a backslash — Verified. Even in a raw string the backslash still escapes the closing quote for the tokenizer, so the literal never terminates.

  4. Does a raw string change the VALUE stored, or only how it is written?

    pch.quizShowAnswer

    B — Only how it is written — the result is an ordinary `str` — `r'...'` is purely a parsing instruction. The object produced is a normal `str` with no memory of how it was spelled.

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading