Replace Single Quotes With Double Quotes in Python

In Python, strings are enclosed with single or double-quotes. Single quotes can be replaced with double quotation marks and vice versa in Python using any of the following methods:

Single Quotes to Double Quotes and Vice Versa

Using str.replace() function

Output:

This "is "an example" string

Using the sub() function in re package

Output:

This is an 'embedded quote' inside main string.

Using str.maketrans()

Output:

"Good', "day

Using the Python JSON module

We use this module to replace single quotes with double quotes in a list of strings.

Output:

["Single1", "Double", "Single2", "Single3"]

Quotation marks (single or double quotations) are used to create string objects in Python. This article will discuss how to replace ‘single’ quotes with “double” quotes.

Quotation Marks in Python

A Python string can only be opened and closed by the same kind of quotation marks, single or double quotes. In other words, in terms of syntax, a string literal terminates the moment the second quotation mark is found, single or double (there is an exception to this. Found out below).

Output:

This is a valid string literal.

Output:

SyntaxError: invalid syntax

The above code runs into a syntax error because the string terminates at the second quote, and therefore Python did not expect the characters coming after that is ab outside”

Having said this, we can see that quotation marks are special characters in Python. However, a backslash (\) can be used as an escape character to convert quotes into literal string characters. For example:

Output:

This section is OK" ab outside

In this example, the second quotation mark has been treated as a regular string character and therefore printed out.

If a quotation mark of a different kind is used inside a string, they are treated as literal string characters.

Output:

This section is 'embedded quotes' outside
This section is "embedded quotes" outside

Replace Single Quotes in Python With Double and Vice Versa [detailed explanation]

In Python, this can be achieved through different methods, for example, using str.replace(), or re.sub(). The inbuilt json package can be used to replace single quotes with double on a list of strings.

Note 1: Replacing quotes in this article means replacing quotation marks within a valid string and not replacing the quotes enclosing the string itself.

Method 1: Using the str.replace() function

Output:

"Good governance is what the people want. " - Governor said.

Output:

This is an 'embedded quote' within the main string.

Method 2: Using the sub() function in re package

The re package in Python captures patterns in strings using regular expressions. The re.sub() function comes in handy in our application. It can be used to find and substitute substrings within the main string. Here are some examples of how re. sub() can be used.

Output:

This is an "embedded quote" inside the main string.

Method 3: Using translate() function in Python

This function, together with str.maketrans(),  replaces one or multiple characters based on definitions made within a valid Python dictionary, as shown below. The translation dictionary must have a single character as the keys.

Output:

book"pencil'ink"pen"rubber

The code snippet above replaces double quotes with single ones and vice-versa.

Method 4: Using the JSON module

This is a special case to replace single quotes on a list of strings with double quotation marks. It is a standard for the json package to use double quotes on strings within a list hence necessitating this replacement.

Output:

["Elijah", "Caroline", "Tomasz", "Smith"]

Output:

"Elijah"

Note that there are double quotes in the output in the second case. This is because quotes are part of the string in this case. In normal practice, json replaces single quotes with double quotes on lists of springs like, as shown in the first example. The second case is rarely used because it has no commonly used case.