Python String – Escape single quote

Python String – Escape single quote

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 these characters inside the string? Fortunately you can do that, and we shall see how to do that.

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

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

1. Escaping single quote in string by using a backslash (\) before the single quote

You can always escape a single quote character by using a backslash character before the single quote.

'Hello \'World\'.'

This tells Python to treat the single quote 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 single quote characters inside the string which we need to escape using backslash character.

Steps

  1. Write the string literal in single quotes, and escape the single 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 single quote characters escaped.

Program

The complete Python program that escapes single quote characters in the string using backslash character.

Python Program

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

Output

Hello 'World'.

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

The single quotes inside the double-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 single quote characters which we need to escape by using double quotes to define the string.

Steps

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

Program

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

Python Program

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

Output

Hello 'World'.

3. Escape single quote by mixing the above two approaches

You can escape single quote characters by using backslash character before the single quote, 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.

Program

The Python program that escapes single quote characters inside the string while defining the string using double quotes.

Python Program

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

Output

Hello 'World'.

Summary

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

Related Tutorials

Code copied to clipboard successfully 👍