Python String – Escape double quotes

Python String – Escape double quotes

In Python, single or double quotes are used as string delimiters. Meaning single or double quotes define where a string starts, and where a string ends. When they define the starting and ending of a string, how do you include a double quotes character inside the string? Fortunately you can do that, and we shall see how to do that.

To escape double quotes character(s) inside a string in Python, you can use backslash character before each double quotes character in the string, or define the string inside single quotes.

In this tutorial, you shall learn how to escape a double quotes within a string in three different approaches. Let us go through each of them in detail.

1. Escaping double quotes in string by using a backslash (\) before the double quotes

You can always escape a double quotes character by using a backslash character before the double quotes.

"Hello \"World\"."

This tells Python to treat the double quotes as a literal character, as part of the string.

For example, consider the following program, where we need to have a string Hello "World". There are two double quotes characters inside the string which we need to escape using backslash character.

Steps

  1. Write the string literal in double quotes, and escape the double quotes inside the string by placing a backslash character just before each of them.
  2. Assign it to a variable, say x. Now, you have a string with double quotes characters escaped.

Program

The complete Python program that escapes double quotes characters in the string using backslash character.

Python Program

x = "Hello \"World\"."
print(x)
Run Code Copy

Output

Hello "World".

2. Escaping double quotes by using single quotes to define the string

The double quotes characters inside the single-quoted string are treated as literal characters without needing to escape them.

'Hello "World".'

For example, consider the following program, where we need to have a string Hello "World". There are two double quotes characters which we need to escape by using single quotes to define the string.

Steps

  1. Define the string literal using single quotes.
  2. Assign it to a variable, say x. Now, you have a string with double quotes characters as literal characters in the string.

Program

The complete Python program that escapes double quotes characters inside the string by defining the string using single quotes.

Python Program

x = 'Hello "World".'
print(x)
Run Code Copy

Output

Hello "World".

3. Escape double quotes by mixing the above two approaches

You can escape double quotes characters by using backslash character before the double quotes, independent of whether you are using single quotes or double quotes as string delimiters.

"Hello \"World\"."
'Hello \"World\".'

Both of these variations produce the same required string Hello "World"..

Program

The Python program that escapes double quotes characters using backslash inside the string when defining the string using single quotes.

Python Program

x = 'Hello \"World\".'
print(x)
Run Code Copy

Output

Hello "World".

Summary

In this tutorial, we learned how to escape double quotes characters inside a string in Python, using different approaches, and examples for each of the approaches.

Related Tutorials

Code copied to clipboard successfully 👍