Python: replace Single with Double Quotes in String or List

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
4 min

banner

# Table of Contents

  1. Replacing single with double quotes in a String
  2. Replace single with double quotes in a List in Python
  3. Replace single with double quotes in a List using str.replace()
  4. Replacing single with double quotes in a String using split() and join()

# Replacing single with double quotes in a String

Use the str.replace() method to replace the single quotes with double quotes in a string.

The method will return a new string where each occurrence of a single quote is replaced with a double quote.

main.py
# โœ… replaces single with double quotes string = "'bobby' 'hadz' 'com'" result = string.replace("'", '"') print(result) # ๐Ÿ‘‰๏ธ "bobby" "hadz" "com"

replace single with double quotes in string

The code for this article is available on GitHub

Notice that we alternate between double and single quotes in the arguments we passed to str.replace().

This is necessary to avoid terminating the string prematurely.

If you had to replace the double quotes with single quotes in a string, you would still have to alternate between quotes in the call to replace().

main.py
# โœ… Replaces double with single quotes string = '"bobby" "hadz" "com"' result = string.replace('"', "'") print(result) # ๐Ÿ‘‰๏ธ 'bobby' 'hadz' 'com'

replace double with single quotes

The str.replace() method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.

The method takes the following parameters:

NameDescription
oldThe substring we want to replace in the string
newThe replacement for each occurrence of old
countOnly the first count occurrences are replaced (optional)
main.py
string = "'bobby' 'hadz' 'com'" result = string.replace("'", '"') print(result) # ๐Ÿ‘‰๏ธ "bobby" "hadz" "com"

The method doesn't change the original string. Strings are immutable in Python.

The strings in the new list only contain double quotes.

# Replace single with double quotes in a List in Python

You can use the json.dumps() method to replace the single with double quotes in a list.

main.py
import json my_list = ['bobby', 'hadz', 'com'] # โœ… Replace single with double quotes in a list (json.dumps()) json_str = json.dumps(my_list) print(json_str) # ๐Ÿ‘‰๏ธ '["bobby", "hadz", "com"]' # ------------------------------------------- # โœ… Replace single with double quotes in a list (str.replace()) my_list = ["'bobby'", "'hadz'", "'com'"] result = [item.replace("'", '"') for item in my_list] print(result) # ๐Ÿ‘‰๏ธ ['"bobby"', '"hadz"', '"com"']

replace single with double quotes in list

The code for this article is available on GitHub

The first example uses the json.dumps() method to replace the single quotes in a list with double quotes.

The json.dumps() method converts a Python object to a JSON formatted string.

main.py
import json my_list = ['bobby', 'hadz', 'com'] # ๐Ÿ‘‡๏ธ json.dumps() converts a Python object to JSON string json_str = json.dumps(my_list) print(json_str) # ๐Ÿ‘‰๏ธ ["bobby", "hadz", "com"] print(type(json_str)) # ๐Ÿ‘‰๏ธ <class 'str'>
The strings in a valid JSON object are wrapped in double quotes, so all strings in the list automatically get wrapped in double quotes.

The json.dumps() method returns a string that is the JSON representation of the list.

If you need to convert the string to a native Python list, use the json.loads() method.

main.py
import json my_list = ['bobby', 'hadz', 'com'] json_str = json.dumps(my_list) print(json_str) # ๐Ÿ‘‰๏ธ ["bobby", "hadz", "com"] native_list = json.loads(json_str) print(native_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com']

The json.loads method parses a JSON string into a native Python object.

Alternatively, you can call the str.replace() method on each item in the list.

# Replace single with double quotes in a List using str.replace()

This is a three-step process:

  1. Use a list comprehension to iterate over the list.
  2. Use the str.replace() method to replace single with double quotes in each string.
  3. The strings in the new list will only contain double quotes.
main.py
my_list = ["'bobby'", "'hadz'", "'com'"] result = [item.replace("'", '"') for item in my_list] print(result) # ๐Ÿ‘‰๏ธ ['"bobby"', '"hadz"', '"com"']

replace single with double quotes in list with str replace

The code for this article is available on GitHub

We used a list comprehension to iterate over the list.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we use the str.replace() method to replace all single quotes in the string with double quotes.

main.py
my_str = "bobby 'hadz' com" result = my_str.replace("'", '"') print(result) # ๐Ÿ‘‰๏ธ bobby "hadz" com

Notice that we alternate between double and single quotes in the string.

This is necessary to avoid terminating the string prematurely.

# Replacing single with double quotes in a String using split() and join()

You can also use the str.split() and str.join() methods to replace the single with double quotes in a string.

main.py
string = "'bobby' 'hadz' 'com'" new_string = '"'.join(string.split("'")) print(new_string) # "bobby" "hadz" "com"

replace single with double quotes with split and join

The code for this article is available on GitHub

The str.split() method splits the string into a list of substrings using a delimiter.

The method takes the following 2 parameters:

NameDescription
separatorSplit the string into substrings on each occurrence of the separator
maxsplitAt most maxsplit splits are done (optional)

We split the string on each occurrence of a single quote.

main.py
string = "'bobby' 'hadz' 'com'" # ๐Ÿ‘‡๏ธ ['', 'bobby', ' ', 'hadz', ' ', 'com', ''] print(string.split("'"))

The last step is to join the list of strings with a double quote separator.

main.py
string = "'bobby' 'hadz' 'com'" new_string = '"'.join(string.split("'")) print(new_string) # "bobby" "hadz" "com"

The str.join() method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev