Python String Escape Quotes

Programing languages are quite friendly with quotes. Almost all programming languages use quotes to get input from the user, print a string or just a value, or assign a string to a variable. Whatever the need is, quotes always come in handy in all situations, but it becomes quite a hassle when it comes to printing quotes itself. If we put quotes inside a string, a run time error will be generated.

For example, if we try to execute a print statement with extra quotes in it that we want to be printed with the string, print(“Python is a very “easy” language”), it will generate a run time error. This is because Python will take “Python is a very ” as one string followed by an ‘easy’ word which is not in a string, followed by a second string “language”. This is not what we intended, but Python understands it this way. The reason is “escape” quotes are special characters in python or any other programming language.

However, nothing is impossible in the world of programming. This tutorial will guide us on how we can put quotes in a string. There are various ways of including quotes character “” inside of a string, though we are going to discuss the three easiest ways for python string escape quotes. Let us begin the learning process.

Example 1

Python has the simplest way of putting quotes in a string: putting a string in a single quote ‘’ which is commonly known as apostrophe and putting double quotes within the string. This way, the compiler will not generate an error and print the double quotes “” within a string. Start a string with a single quote ‘, add the string with escape quotes and end the string with another single quote ‘ like this; print(‘Python is a very “easy” language’).

This way, Python will print the escape quotes without generating any error. Here is an example code to print the quotes within a string. We can assign a string to a variable and then print the variable, or we simply execute the print command containing the string; both will generate the same output. Let us see the code.

s = ‘Python is a very “easy” language’


print(‘Python is a very “easy” language’)

The output of the above displayed code is as follow:

Example 2

In the next example, we are going to learn the use of backslash characters followed by any type of quotes, i.e., single, or double, to print the quotes in a string. When a backslash is used with a special character, the python simply prints the special character and discards the backslash character. This concept is recognized as an escape sequence. The backslash \ character followed by any special character will be printed as it is, even the backslash itself.

For example, we want to print a backslash within a string, so we need to escape it with another backslash; in other words, put \\ in a string like this, print(“Print the backslash \\”). Here is the code for string escape quotes using the backslash character. Again, the string can be put into a variable, and that variable can be printed or simply execute the print command containing the string to be printed.

s = “Python is a very \“easy\” language”


print(“Python is a very \“easy\” language”)

Here is the output of the code given above.

Example 3

Another simple way to deal with escape quotes is to put triple quotes around the string. Once we put the string within the triple quotes, we can print any special character without getting any error. Here is the example code:

s = """Python is a very “easy” language"""


print("""Python is a very “easy” language""")

The output of the above code will be:


Here, we have learned about python string escape quotes. We have gone through different methods for a string escape sequence. The first one is simply putting the string with escape quotes inside single quotes and the second way is to use the backslash special character with other special characters to get them printed in a string. Lastly, we used an example in which we put tripe quotes around the string.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content