Python List Replace Single with Double Quotes Example

By Hardik Savani October 30, 2023 Category : Python

Hi Guys,

This simple article demonstrates of python list replace single quotes with double. we will help you to give an example of python list replace single quote with double quote. In this article, we will implement a how to replace double quote to single quotes in python list. I explained simply step by step python list replace single with double quotes.

In this example, i will give you several examples of replacing single quotes with double quotes in python. we will use replace() function and dumps() of json library in python. So, without further ado, let's see simple examples: You can use these examples with python3 (Python 3) version.

Example 1: Python List Replace Single Quotes with Double Quotes

main.py

import json
  
myList = ['It', 'Solution', 'Stuff', 'com']
  
# python list replace single quotes with double
newList = json.dumps(myList)
  
print(newList);

Output:

["It", "Solution", "Stuff", "com"]

Example 2: Python List Replace Double Quotes with Single Quotes

main.py

import json
  
myList = ["It", "Solution", "Stuff", "com"]
  
# python list replace double quotes with single
newList = [item.replace("'", '"') for item in myList]
  
print(newList);

Output:

['It', 'Solution', 'Stuff', 'com']

Example 3: Python String Replace Single Quotes with Double Quotes

main.py

myString = "It 'Solution' Stuff com"
  
# python string replace single quotes with double
newString = myString.replace("'", '"')
   
print(newString)

Output:

It "Solution" Stuff com

Example 4: Python String Replace Double Quotes with Single Quotes

main.py

myString = 'It "Solution" Stuff com'
   
# python string replace double quotes with single
newString = myString.replace('"', "'")
   
print(newString)

Output:

It 'Solution' Stuff com

I hope it can help you...

Tags :
Shares