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

str1 = "This 'is 'an example string"
a = str1.replace("'", "\"")
print(a)

Output:

This "is "an example" string

Using sub() function in re package

import re 
str1 =  'This is an "embedded quote" inside main string.'
str1_replaced = re.sub("\"", "'", str1)
print(str1_replaced)

Output:

This is an 'embedded quote' inside main string.

Using str.maketrans()

string_trans = "'Good\", 'day"
# Hope you know why we had to escape (\) double quotes and not single
# Discussed earlier
stringn1 = string_trans.translate(str.maketrans({'"': '\'', "'": "\""}))
print(stringn1)

Output:

"Good', "day

Using the Python JSON module

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

import json
lst1 = ['Single1', "Double", 'Single2', 'Single3']
lst2 = json.dumps(lst1)
print(lst2)

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).

valid_string = "This is a valid string literal." # You can use single quotes here
print(valid_string)

Output:

This is a valid string literal.
invalid_s = "This section is OK" ab outside"
print(invalid_s)

Output:

SyntaxError: invalid syntax

The above code runs into 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:

escaped_quote = "This section is OK\" ab outside"
print(escaped_quote)

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.

embedded_quote1 = "This section is 'embedded quotes' outside"
embedded_quote2 = 'This section is "embedded quotes" outside'
print(embedded_quote1)
print(embedded_quote2)

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 a 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

string1 = "'Good governance is what the people want. ' - Governor said"
# backslash makes a quotation marks literal string character
# quotation mark of a different kind is considered a normal string
# when it appears between validation quotation marks
print(string1.replace("'", "\""))

Output:

"Good governance is what the people want. " - Governor said.
string2 = 'This is an "embedded quote" within the main string.'
print(string2.replace('"', "'"))

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.

import re
string_re = "This is an 'embedded quote' inside the main string."
substring_replaced = re.sub("'", "\"", string_re)
print(substring_replaced)

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.

string_trans = "book'pencil\"ink'pen'rubber"
# Hope you know why we had to escape double quotes and not single
# Discussed earlier
stringn1 = string_trans.translate(str.maketrans({'"': '\'', "'": "\""}))
print(stringn1)

Output:

book"pencil'ink"pen"rubber

The code snippet above replaces double quotes with single 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 json package to use double quotes on strings within a list hence necessitating this replacement.

b_list = ['Elijah', "Caroline", "Tomasz", "Smith"]
b_list = json.dumps(b_list)
print(b_list)

Output:

["Elijah", "Caroline", "Tomasz", "Smith"]
b_list1 = 'Elijah'
b_list1 = json.dumps(b_list1)
print(b_list1)

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.