Last updated: Apr 9, 2024
Reading timeยท4 min
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.
# โ replaces single with double quotes string = "'bobby' 'hadz' 'com'" result = string.replace("'", '"') print(result) # ๐๏ธ "bobby" "hadz" "com"
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()
.
# โ Replaces double with single quotes string = '"bobby" "hadz" "com"' result = string.replace('"', "'") print(result) # ๐๏ธ 'bobby' 'hadz' 'com'
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:
Name | Description |
---|---|
old | The substring we want to replace in the string |
new | The replacement for each occurrence of old |
count | Only the first count occurrences are replaced (optional) |
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.
You can use the json.dumps()
method to replace the single with double quotes
in a list.
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"']
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.
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 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.
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.
This is a three-step process:
str.replace()
method to replace single with double quotes in each
string.my_list = ["'bobby'", "'hadz'", "'com'"] result = [item.replace("'", '"') for item in my_list] print(result) # ๐๏ธ ['"bobby"', '"hadz"', '"com"']
We used a list comprehension to iterate over the list.
On each iteration, we use the str.replace()
method to replace all single
quotes in the string with double quotes.
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.
You can also use the str.split()
and str.join()
methods to replace the
single with double quotes in a string.
string = "'bobby' 'hadz' 'com'" new_string = '"'.join(string.split("'")) print(new_string) # "bobby" "hadz" "com"
The str.split() method splits the string into a list of substrings using a delimiter.
The method takes the following 2 parameters:
Name | Description |
---|---|
separator | Split the string into substrings on each occurrence of the separator |
maxsplit | At most maxsplit splits are done (optional) |
We split the string on each occurrence of a single quote.
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.
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.
You can learn more about the related topics by checking out the following tutorials: