Remove double quotes from a List of strings in Python

avatar

Borislav Hadzhiev

Last updated: Aug 18, 2022

banner

Photo from Unsplash

Remove double quotes from a List of strings in Python #

To remove the double quotes from a list of strings:

  1. Use a list comprehension to iterate over the list.
  2. Use the str.replace() method to remove the double quotes from each string.
  3. The items in the new list won't contain double quotes.
main.py
my_list = ['"one"', '"two"', '"three"'] # ✅ Remove all quotes from a list of strings new_list = [item.replace('"', '') for item in my_list] print(new_list) # 👉️ ['one', 'two', 'three'] # ------------------------------------------------- # ✅ Remove the leading and trailing quotes from a list of strings new_list = [item.strip('"') for item in my_list] print(new_list) # 👉️ ['one', 'two', 'three']

We used a list comprehension to remove the double quotes from a list of strings.

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 remove all occurrences of a double quote by replacing each with an empty string.

The list comprehension returns a new list, in which the strings don't contain double 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)

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

Alternatively, you can use the str.strip() method.

To remove the double quotes from a list of strings:

  1. Use a list comprehension to iterate over the list.
  2. Use the str.strip() method to remove the leading and trailing double quotes from each string.
  3. The items in the new list won't contain leading and trailing double quotes.
main.py
my_list = ['"one"', '"two"', '"three"'] new_list = [item.strip('"') for item in my_list] print(new_list) # 👉️ ['one', 'two', 'three']

The example uses the str.strip() method to remove the leading and trailing double quotes from each string in the list.

The str.strip method takes a string containing characters as a parameter and removes all occurrences of the characters from the front and back of the string.

We passed a double quote to the strip() method to remove all occurrences of a double quote at the front and end of each string.

The strip() method doesn't remove all occurrences of the specified character from the string, it only removes the occurrences at the front and end.

main.py
# 👇️ 'hello " world' print('"hello " world"'.strip('"'))

You can also use the str.lstrip() and str.rstrip() methods, which remove the leading or the trailing occurrences of the specified character.

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.