How to Escape Quotes From String in Python
Last Updated :
03 Jan, 2023
The single or double quotation is a unique character that can be used in a Python program to represent a string by encapsulating it in matching quotes. When the quotes are used inside a string contained in similar quotes it confuses the compiler and as a result, an error is thrown.
This article will demonstrate how to escape quotes from a string in Python.
Escape from single quotes in a string in Python
Wrong way:
Python3
print('Hello I don't dislike python')
Output:
print('Hello I don't dislike python')
^
SyntaxError: invalid syntax
Right way:
Instead of using single quotations, enclose the string in double quotes.
Python3
print("Hello I don't dislike python")
Output:
Hello I don't dislike python
Before the single quotation in the string, insert the escape character.
Python3
print('Hello I don\'t dislike python')
Output:
Hello I don't dislike python
Escape from double quotes in a string in Python
Wrong way:
Python3
print("He said, "he likes python"")
Output:
print("He said, "he likes python"")
^
SyntaxError: invalid syntax
Right way:
Instead of using double quotations, enclose the string in single quotes.
Python3
print('He said, "he likes python"')
Output:
He said, "he likes python"
Before the double quotation in the string, insert the escape character.
Python3
print("He said, \"he likes python\"")
Output:
He said, "he likes python"
Escape using triple quote
Triple quotes (or docstrings as they are called in official python documentation) can be used to represent multiple strings containing quotes.
Python3
print('''
Hello I don't dislike python
He said, "he likes python"
''')
Output:
Hello I don't dislike python
He said, "he likes python"
Using the ‘r’ keyword to specify a raw string
In python, the 'r' keyword can be used to indicate a raw string to the compiler. This is rather helpful in special cases like taking input from the user.
Python3
# Using single quote
print(r"This is '(single quote)'")
# Using double quote
print(r'This is "(double quote)"')
# Using both single and double quote
print(r"These are '(single quote)' and " r'"(double quote)"')
Output:
This is '(single quote)'
This is "(double quote)"
These are '(single quote)' and "(double quote)"
Similar Reads
How To Escape Strings in JSON? JSON (JavaScript Object Notation) is a popular data format that is widely used in APIs, configuration files, and data storage. While JSON is straightforward, handling special characters within strings requires some extra care. Certain characters must be escaped to be valid within a JSON string.Table
2 min read
How to Convert Bytes to String in Python ? We are given data in bytes format and our task is to convert it into a readable string. This is common when dealing with files, network responses, or binary data. For example, if the input is b'hello', the output will be 'hello'.This article covers different ways to convert bytes into strings in Pyt
2 min read
How to Urlencode a Querystring in Python? URL encoding a query string consists of converting characters into a format that can be safely transmitted over the internet. This process replaces special characters with a '%' followed by their hexadecimal equivalent. In this article, we will explore three different approaches to urlencode a query
2 min read
Convert Set to String in Python Converting a set to a string in Python means changing a group of unique items into a text format that can be easily read and used. Since sets do not have a fixed order, the output may look different each time. For example, a set {1, 2, 3} can be turned into the string "{1, 2, 3}" or into "{3, 1, 2}"
2 min read
How to Escape Double Quotes in JSON? JSON (JavaScript Object Notation) is a popular lightweight format used to store and transmit data objects. A typical JSON object consists of key-value pairs where keys are strings enclosed in double quotes. While using double quotes within JSON strings, it can create issues as they can be misinterpr
3 min read
How to remove double quotes from String in Ruby? In this article, we will discuss how to remove double quotes from a string using Ruby. We can remove double quotes from strings through different methods ranging from using gsub Method with Regular Expression to delete Method Table of Content Removing double quotes from string using gsub Method with
2 min read