Introduction
There are several ways you can remove quotes from a string in Python. You may need to remove all quotes or just ones surrounding a string. You may also need to remove single or double quotes.
In this short article, we've compiled a comprehensive list of methods you can use to remove quotes from a string in Python. It's absolutely up to you which one you'll use. Please note that the solutions we've listed here are not ordered from best to worst. Each solution is good as long as it meets your needs in a specific case.
How to Remove All Quotes from a String in Python
First of all, let's take a look at how to remove all quotes from a string. Solutions listed in this section can be altered to work with both single and double quotation marks. But, for the purpose of this article, let's say we have an example string that has three double quotation marks in it:
example_str = '"This is a string" with quotes"'
In this section, we'll take a look at several ways to remove all "
from the example_str
.
str.replace()
The first approach we'll discuss is to use the str.replace()
method on the example_str
. It accepts two arguments - the string we want to replace and the replacement string. In this case, we'll replace a double quotation mark ("
) with an empty string:
new_str = example_str.replace('"', '')
print(new_str) # This is a string without quotes
The str.replace()
used in this way will remove all double quotes from the example_str
.
Regular Expressions
This approach uses regular expressions to remove all quotes from a string. Before we start using regular expressions in Python, we first need to import the re
module:
import re
After that, we can use the re.sub()
method to substitute all occurrences of the quotation mark with the empty string:
new_str = re.sub('"', '', example_str)
print(new_str) # This is a string without quotes
This will give us the example_str
without any quotes in it.
str.join()
The str.join()
is another method we can use to remove all quotes from a string in Python. This solution has a bit more complex syntax than other solutions if you are not already familiar with the join()
method. We'll essentially go over the example_str
character-by-character and append each which is not a quotation mark to the empty string. That way, we'll get the example_str
without any quotes in it:
new_str = ''.join(c for c in example_str if c not in '"')
print(new_str) # This is a string without quotes
How to Remove Quotes Surrounding a String
So far, we've been discussing several approaches on how to remove all quotation marks from a string in Python. That's only one way of looking at removing quotes from a string. Arguably, the more common problem is how to remove only quotes surrounding a string. The str.strip()
method is intended to do just that. Therefore, we'll discuss the str.strip()
method in this section.
Say we have the same exact example_str
as in the previous section, and we want to remove only the first and the last quotation marks:
example_str = '"This is a string" with quotes"'
Using the str.strip()
with a double quotation mark as its argument will remove leading and trailing quotation marks from the example_str
:
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
new_str = example_str.strip('"')
print(new_str) # This is a string" without quotes
Note: No matter how many leading and/or leading quotation marks are in the string, the str.strip()
will remove all of them.
Besides the usual str.strip()
method, there are also two of its derivatives - str.lstrip()
and str.rstrip()
. The first one removes all leading quotes, and the second removes all trailing quotes:
new_str = example_str.lstrip('"')
print(new_str) # This is a string" without quotes"
new_str = example_str.rstrip('"')
print(new_str) # "This is a string" without quotes
Conclusion
In this short article, we've taken a comprehensive look at how to remove quotes from a string in Python. First of all, we've explained how to remove all quotes from a string using several different methods - str.replace()
, str.join()
, and re.sub()
. Afterward, we've taken a look at the str.strip()
method, as well as its derivatives str.lstrip()
and str.rstrip()
.