Escape quotes in Python

Escape quotes in Python

💡 Outline
You can use \ to escape quotes in Python.
If you want to delay python program by 500 ms, then you need to pass 0.5 as parameter to sleep method.

Escape sequences are frequently used in programming. They are used to convey to the compiler that the escape character has some other alternative meaning and is not be treated in the traditional way.

In Python, the backslash character \ is used to convey escape sequences. The character that we want to escape is added immediately after this backslash.

In this article, we will discuss how to escape quotes in Python.

Using \ to escape quotes in Python

Now imagine that you have a string that you wish to print. This string contains a single quote as an apostrophe. The compiler will interpret this differently and raise an error and show syntax is invalid.

For example,

Now since Python uses both single quotes and double quotes to store string value, we can use double quotes and the above problem would go away and if we deal with the same issue for double quotes then we can enclose them in single quotes. However, this is not an ideal solution to the problem.

For such situations, we can use escape sequences. We can use both single quotes or double quotes after a backlash character to include them in a string.

See the following code.


Don’t use this way Sample “Sample” Sample

Using \ with replace() to escape quotes in Python

We can also use the replace() function to insert escape quotes in a string. We simply replace the quotes with \".

For example,


sample “sample” sample

Using json.dumps to escape quotes in Python

Even the json.dumps() function from the json library, takes a string and returns a new string by adding two backslashes wherever it encounters double-quotes. Since two backslashes are added only one is considered as an escape sequence and the other gets printed.

For example,


“sample \”sample\” sample”

We can use different combinations of characters with the \ to specify alternative meanings. For example, \n conveys a new line, \t creates a horizontal tab etc.

Using triple single or double quotes to escape quotes in python

There is another way to avoid this error and include the quotes in a string without using escape quotes. In Python, we can use triple single or double quotes to initiate multi-line strings. In multi-line strings we do not face this error.

For example,



The above method adds a space on the top and bottom of the string which can be removed by adding a backslash \ at the start and end of a string.

Use r to include \ and quotes in String

Now. what if we wish to include \ or quotes in the final string. For such cases, we can use the r keyword before specifying the string. The r keyword tells the compiler that it is dealing with a raw string. A raw string ignores all the formatting and escape sequences.

For example,


sample \’sample\’ sample

As you can see above, we were able to include the backslashes and everything in the final string.

That’s all about how to escape quotes in Python.

Was this post helpful?


You may also like:

Related Posts

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.