How to remove Quotes from a List of Strings in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
4 min

banner

# Remove quotes from a List of Strings in Python

To remove the 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 quotes from each string.
  3. The items in the new list won't contain quotes.
main.py
my_list = ['"bobby"', '"hadz"', '"com"'] # โœ… Remove all quotes from list of strings new_list = [item.replace('"', '') for item in my_list] print(new_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com'] # ------------------------------------------------- # โœ… Remove leading and trailing quotes from list of strings new_list = [item.strip('"') for item in my_list] print(new_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com']

remove quotes from list of strings

The code for this article is available on GitHub

We used a list comprehension to remove the 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 quotes.

# Removing the single quotes from a List of strings

The examples remove the double quotes from the strings in the list, but you can use the same approach to remove the single quotes from each string.

main.py
my_list = ["'bobby'", "'hadz'", "'com'"] new_list = [item.replace("'", '') for item in my_list] print(new_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com']

removing single quotes from list of strings

The code for this article is available on GitHub

The example above removes all occurrences of a single quote from each string in the list.

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.

We remove all quotes from each string in the list by replacing each quote with an empty string.

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

# Remove quotes from a List of strings using str.strip()

This is a three-step process:

  1. Use a list comprehension to iterate over the list.
  2. Use the str.strip() method to remove the leading and trailing quotes from each string.
  3. The items in the new list won't contain leading and trailing quotes.
main.py
my_list = ['"bobby"', '"hadz"', '"com"'] new_list = [item.strip('"') for item in my_list] print(new_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com']

remove quotes from list of strings using str strip

The code for this article is available on GitHub

The example uses the str.strip() method to remove the leading and trailing 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.

# Removing the leading or trailing quotes from a List of Strings

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

main.py
my_list = ['"bobby"', '"hadz"', '"com"'] new_list = [item.lstrip('"') for item in my_list] print(new_list) # ๐Ÿ‘‰๏ธ ['bobby"', 'hadz"', 'com"'] new_list = [item.rstrip('"') for item in my_list] print(new_list) # ๐Ÿ‘‰๏ธ ['"bobby', '"hadz', '"com']

remove leading or trailing quotes from list of strings

The code for this article is available on GitHub

The str.lstrip() method takes a string containing characters as an argument and returns a copy of the string with the specified leading characters removed.

The str.rstrip() method takes a string containing characters as an argument and returns a copy of the string with the specified trailing characters removed.

# Remove the quotes from a String using a generator expression

You can also use a generator expression to remove the quotes from a string.

main.py
my_str = "bobby 'hadz' com 'abc' xyz" result = ''.join(char for char in my_str if char != "'") print(result) # ๐Ÿ‘‰๏ธ bobby hadz com abc xyz
The code for this article is available on GitHub

We used a generator expression to iterate over the string.

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

On each iteration, we check if the current character is not equal to a single quote and return the result.

The output is a generator object that contains the characters of the string excluding the single quotes.

The last step is to use the str.join() method to join the remaining characters into a string.

main.py
my_str = "bobby 'hadz' com 'abc' xyz" result = ''.join(char for char in my_str if char != "'") print(result) # ๐Ÿ‘‰๏ธ bobby hadz com abc xyz

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

The string the method is called on is used as the separator between the elements.

For our purposes, we called the str.join() method on an empty string to join the remaining characters without a separator.

# 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