Python Central HubSign in

Python Programming / Python Strings

Python Escape String

How to escape a string in Python. Learn how to use escape characters in Python to manipulate text, control formatting, and handle special characters.

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.

Basics of Escape Characters

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.

Escape Characters in Python

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

Newline Escape 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:

Python
# print string on multiple lines
print('This is a string\non multiple lines')

Output

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

Tab Escape Character

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:

Python
# print string with tab
print('This is a string\twith a tab')

Output

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

Carriage Return Escape Character

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:

Python
# print string on multiple lines
print('This is a string\ron multiple lines')

Output

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

Backspace Escape Character

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:

Python
# print string with backspace
print('This is a string\bwith a backspace')

Output

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

Form Feed Escape Character

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:

Python
# print string on multiple lines
print('This is a string\fon multiple lines')

Output

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

Vertical Tab Escape Character

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:

Python
# print string with vertical tab
print('This is a string\vwith a vertical tab')

Output

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

Backslash Escape Character

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:

Python
# print string with backslash
print('This is a string\\with a backslash')

Output

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

Single Quote Escape Character

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:

Python
# print string with single quote
print('This is a string\'with a single quote')

Output

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

Double Quote Escape Character

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:

Python
# print string with double quote
print('This is a string\"with a double quote')

Output

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

Bell Escape Character

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:

Python
# print string with bell
print('This is a string\awith a bell')

Output

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

Null Byte Escape Character

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:

Python
# print string with null byte
print('This is a string\0with a null byte')

Output

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

Unicode Character Escape Character

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:

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

Output

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

Hexadecimal Character Escape 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:

Python
# print string with hexadecimal character
print('This is a string\x24with a hexadecimal character')

Output

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

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:

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

Output

cmd
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

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:

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

Output

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

Octal Character Escape 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:

Python
# print string with octal character
print('This is a string\044with an octal character')

Output

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

Conclusion

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!


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.

Check yourself

quizCheck yourself
  1. What is `len('a b')` and what is `len(r'a b')`?

    Show answer

    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?

    Show answer

    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?

    Show answer

    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?

    Show answer

    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.

Try it: Escape Character Exercises

Exercise 1 – Newline and Tab

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Quotes Inside Strings

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Raw String

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did