Remove Quotes from a String in Python

As a Python developer, I was working on a data-cleaning project where I had to process large CSV files exported from a U.S. retail system. The issue was that many of the text fields came wrapped in single or double quotes.

At first, I thought Python would have a built-in way to strip quotes from strings. But it turns out, there isn’t a single “one-click” solution. Instead, we can use a few simple methods to handle this quickly.

In this tutorial, I’ll walk you through five easy ways to remove quotes from a string in Python. I’ll share the exact code I use in my projects so you can apply it right away.

Method 1: Use str.replace in Python

In Python, the str.replace() method is a simple way to replace all occurrences of a substring within a string. To remove all double quotes from a string in Python, we replace them with an empty string.

Consider the following example:

input_string = "Always Keep a Positive Mindset"
resultant_string = input_string.replace('"', '')

print(resultant_string)

I want to replace(“”) from the first argument, and then the second argument is the new substring, an empty string in Python.

Output:

Always Keep a Positive Mindset

You can refer to the screenshot below to see the output.

How to remove quotes from a string in Python

I used this input_str.replace the (‘”‘, ”) line of code to initiate the replacement process.

Method 2: Use Regular Expression

Python provides a re module for complex string manipulation. It allows for pattern matching and can remove double quotes under specific conditions or patterns.

import re

thought = "Believe you can you're halfway there."
output_string = re.sub('"', '', thought)

print(output_string)
output_string = re.sub('"', '', thought)

Output:

Believe you can you're halfway there.

You can refer to the screenshot below to see the output.

Python program to remove quotes from a string using Regular Expression

The re.sub() function from the re module in Python replaces occurrences of a pattern with the first argument in a string and the third argument with a replacement string, i.e., the second argument.

Method 3: Use the str.translate() with str.maketrans()

The str.translate() method in Python is used for removing specific characters from a string. We use it with str.maketrans() to remove double quotes from a string.

input_string = "Nothing is impossible. The word itself says 'I'm possible!"
translator = str.maketrans('', '', "'")
output_string = input_string.translate(translator)
print(output_string)
translator = str.maketrans('', '', "'")
output_string = input_string.translate(translator)

Output:

Nothing is impossible. The word itself says Im possible!

You can refer to the screenshot below to see the output.

Remove quotes from a string in Python

In the str.maketrans() function, the argument is a string of characters to be removed from the source string. Along with this, we used the str.translate() function to remove the double quotes in Python.

Method 4: Use list comprehension

List comprehension is a brief way to process elements in a collection. We can use the approach to remove quotes from a string using list comprehension.

input_text = '"Stay focused desciplined, and determined always"'

result = ''.join([char for char in input_text if char != '"'])
print(result)

Output:

Stay focused desciplined, and determined always

You can refer to the screenshot below to see the output.

Remove quotes from a string using list comprehension in Python

This method uses list comprehension to filter out quotation marks and join the remaining characters into a clean string.

Method 5: Use filter() method in Python

Python’s filter() function can filter out characters we don’t want in our string, like double quotes.

input_quote = "Hard Work Is The Formula For Success"
output_quote = ''.join(filter(lambda x: x != '"', input_quote))

print(output_quote)
output_quote = ''.join(filter(lambda x: x != '"', input_quote))

Output:

Hard Work Is The Formula For Success

This is the full source code of Python to remove a quote from a string using the filter() method.

How to remove quote from a string in Python using filter() method

I used the lambda function in the filter() function for each character in input_quote, keeping only those that are not double quotes, and the join function is used to concatenate the filter characters into a new string.

Conclusion

In this article, I have described the various methods to remove quotes from a string in Python, like str.replace(), regular expression(re), str.translate() with str.maketrans(), list comprehension, and using the filter() method.

I hope this helps you understand the step-by-step procedure to remove quotes from strings with illustrative examples.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.