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.

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\n\nNewlineInserts a new line
2\t\tTabInserts a tab
3\r\rCarriage ReturnInserts a carriage return
4\b\bBackspaceInserts a backspace
5\f\fForm FeedInserts a form feed
6\v\vVertical TabInserts a vertical tab
7\\\\BackslashInserts a backslash
8\'\'Single QuoteInserts a single quote
9\"\"Double QuoteInserts a double quote
10\a\aBellInserts a bell
11\0\0Null ByteInserts a null byte
12\N{name}\N{name}Unicode CharacterInserts a Unicode character
13\xhh\xhhHexadecimal CharacterInserts a hexadecimal character
14\uhhhh\uhhhhUnicode 16-bit Hexadecimal CharacterInserts a Unicode 16-bit hexadecimal character
15\Uhhhhhhhh\UhhhhhhhhUnicode 32-bit Hexadecimal CharacterInserts a Unicode 32-bit hexadecimal character
16\ooo\oooOctal CharacterInserts an octal character

Newline Escape Character

The newline escape character (\n\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')
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
command
C:\Users\Your Name> python strings.py
This is a string
on multiple lines

Tab Escape Character

The tab escape character (\t\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')
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
command
C:\Users\Your Name> python strings.py
This is a string    with a tab

Carriage Return Escape Character

The carriage return escape character (\r\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')
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
command
C:\Users\Your Name> python strings.py
This is a string
on multiple lines

Backspace Escape Character

The backspace escape character (\b\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')
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
command
C:\Users\Your Name> python strings.py
This is a stringwith a backspace

Form Feed Escape Character

The form feed escape character (\f\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')
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
command
C:\Users\Your Name> python strings.py
This is a string
on multiple lines

Vertical Tab Escape Character

The vertical tab escape character (\v\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')
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
command
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:

strings.py
# print string with backslash
print('This is 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
command
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:

strings.py
# print string with single quote
print('This is 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
command
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:

strings.py
# print string with double quote
print('This is 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
command
C:\Users\Your Name> python strings.py
This is a string"with a double quote

Bell Escape Character

The bell escape character (\a\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')
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 
command
C:\Users\Your Name> python strings.py
This is a stringwith a bell 

Null Byte Escape Character

The null byte escape character (\0\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')
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
command
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}\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')
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
command
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\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')
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
command
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\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')
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
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

The Unicode 32-bit hexadecimal character escape character (\Uhhhhhhhh\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')
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
command
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\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')
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
command
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!

Was this page helpful?

Let us know how we did