Escape sequences in Python

Escape sequences in Python


An escape sequence or escape character allows you to insert a special character in a string. For example, you can insert a new line in a string using \n character.

The backslash \ is used before the character that you want to escape.

List of escape characters in Python

The following table shows you a list of escape characters or sequences in Python –

Escape sequenceDescription
\'Single quote
\"Double quote
\nNew line
\fForm feed
\aASCII Bell
\rCarriage return
\tHorizontal tab
\vVertical tab
\oooCharacter with octal value ooo
\xhhCharacter with hex value hh

Usage of escape sequence in Python with examples

A string is a collection of characters inside the single or double quote. Now, what if you want to insert single or double quotes in a string?

Using single or double quotes in a string

You can use single quotes inside the string enclosed with double-quotes or double quotes inside the string enclosed in single quotes.

For example –

print("Hey, I'm Lalit")
print('This is a "Hello World!" program in Python')

This will print the strings like given in the image –

But using single quotes inside a string enclosed with single quotes or double quotes inside the string enclosed with double quotes are not allowed this will give you a syntax error.

print('Hey, I'm Lalit')
print("This is a "Hello World!" program in Python")

Here both the statements are wrong and will through the syntax error in Python.

Including backslash in a string

If you want to include a backslash character in a string you need to escape it with a backslash. For example, to print the path of a directory on Windows you will need to use backslash every time you insert a backslash in the string.

Example –


This will print the path with the single backslashes –

Inserting a new line

You can insert a new line by using the \n character in a string. For example –

print("You can create a multiline string \nby using escape sequence in Python")

You can see the output in the image below.

You can use \n multiple times to insert multiple newlines.

Similarly, you can use other escape sequences that are given in the above table.

Python raw string

Python raw string treats backslash character as literal instead of treating it as an escape character. You can create a raw string by prefixing it with r or R.

print(r" Hello \ World!")

This will print the string with the backslash character. Another example is –

print(r" Hello \n World!")

The string will be treated as normal text so instead of inserting a new line, this will print the\n with the string.

But using a backslash at the end of a raw string will through an error.

Ok, that’s all for now. I hope this article given you a basic idea of escape sequences used in the Python programming language.

Previous Python Boolean Data Type
Next Python Strings

About author

You might also like

Python Programming 0 Comments

Python 2 Vs Python 3

Python 2.x and Python 3.x are two major releases of the python programming language. Python 2 was released in 2000 and now in 2020, it is no longer in development.

Python Programming 0 Comments

Scope of variables in Python

A variable in Python is used to store a value in the memory. The value of a variable can be changed later in the program. Python is a dynamically typed

Python Programming 0 Comments

Python Indentation

An indentation or indent is an empty space at the beginning of a line that signals the start of a new logical block in a Python program. Many programming languages


No Comments Yet!

You can be first to comment this post!

Leave a Reply