Remove quotes from a String in Python

avatar

Borislav Hadzhiev

Last updated: Aug 18, 2022

banner

Photo from Unsplash

Remove quotes from a String in Python #

Use the str.replace() method to remove the quotes from a list of strings, e.g. new_str = my_str.replace('"', ''). The replace method will remove the quotes from the string by replacing each quote with an empty string.

main.py
my_str = '"one" two "three" four "five"' # ✅ Remove all quotes from string new_str = my_str.replace('"', '') print(new_str) # 👉️ 'one two three four five' # ✅ Remove leading and trailing quotes from string new_str = my_str.strip('"') print(new_str) # 👉️ 'one" two "three" four "five' # --------------------------------------------------- # ✅ Remove all quotes from a list of strings my_list = ['"one"', '"two"', '"three"'] new_list = [item.replace('"', '') for item in my_list] print(new_list) # 👉️ ['one', 'two', 'three']

The first example uses the str.replace() method to remove all double quotes from a string.

You can use the same approach to remove all single quotes from the string.

main.py
my_str = "'one' two 'three' four 'five'" new_str = my_str.replace("'", '') print(new_str) # 'one two three four five'

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

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 the string by replacing each quote with an empty string.

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

Use the str.strip() method to remove quotes from a string, e.g. new_str = my_str.strip('"'). The str.strip() method will return a new string with all leading and trailing quotes removed.

main.py
my_str = '"one" two "three" four "five"' # ✅ Remove leading and trailing quotes from string new_str = my_str.strip('"') print(new_str) # 👉️ 'one" two "three" four "five' # ✅ Remove leading quotes from string new_str = my_str.lstrip('"') print(new_str) # 👉️ one" two "three" four "five" # ✅ Remove trailing quotes from string new_str = my_str.rstrip('"') print(new_str) # 👉️ "one" two "three" four "five

The example uses the str.strip() method to remove the leading and trailing quotes from a string.

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 the string.

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

If you need to remove all quotes from a list of strings, call the replace() method on each string in the list.

main.py
my_list = ['"one"', '"two"', '"three"'] new_list = [item.replace('"', '') for item in my_list] print(new_list) # 👉️ ['one', 'two', 'three']

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 replace() method to remove all quotes from the current string.

The list comprehension returns a new list, in which the strings don't contain quotes.

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.