To remove quotes from a string using Python, the easiest way is to use the Python replace() function. You can remove single and double quotes using replace().

string_single_quotes = "This' is' a' string' with' quotes."
string_double_quotes = 'This" is" a" string" with" quotes.'

string_without_single = string_single_quotes.replace("'","")
string_without_double = string_double_quotes.replace('"',"")

print(string_without_single)
print(string_without_double)

#Output:
This is a string with quotes.
This is a string with quotes.

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

import re

string_single_quotes = "This' is' a' string' with' quotes."
string_double_quotes = 'This" is" a" string" with" quotes.'

string_without_single = re.sub("["']",'',string_single_quotes)
string_without_double = re.sub("["']",'',string_double_quotes)

print(string_without_single)
print(string_without_double)

#Output:
This is a string with quotes.
This is a string with quotes.

When using string variables in Python, we can easily perform string manipulation to change the value of the string variables.

One such manipulation is to remove characters from a string variable. Quotes, both single and double quotes, can be troubling characters to deal with in string variables.

We can easily remove quotes from a string in Python.

The easiest way to get rid of both single and double quotes in a string using Python is with the Python string replace() function.

The replace() function takes two arguments: the substring we want to replace, and the replacement substring. In this case, to remove quotes, we pass the single (“‘”) or double (‘”‘) quote character as the first argument, and an empty string as the second argument.

Below are some examples of how you can remove quotes from strings in Python using the replace() function.

string_single_quotes = "This' is' a' string' with' quotes."
string_double_quotes = 'This" is" a" string" with" quotes.'

string_without_single = string_single_quotes.replace("'","")
string_without_double = string_double_quotes.replace('"',"")

print(string_without_single)
print(string_without_double)

#Output:
This is a string with quotes.
This is a string with quotes.

Using Regular Expressions to Remove Quotes from Strings in Python

Another way you can remove quotes from strings in Python is with regular expressions.

With the regular expression re module in Python you can remove both single or double quotes from a string easily.

We can easily define a regular expression which will search for both single and double quotes, and then using the sub() function, we will replace those quotes with spaces.

Below is an example of how to use regular expressions to remove quotes from a string using Python.

import re

string_single_quotes = "This' is' a' string' with' quotes."
string_double_quotes = 'This" is" a" string" with" quotes.'

string_without_single = re.sub("["']",'',string_single_quotes)
string_without_double = re.sub("["']",'',string_double_quotes)

print(string_without_single)
print(string_without_double)

#Output:
This is a string with quotes.
This is a string with quotes.

Hopefully this article has been useful for you to learn how to remove quotes from strings using Python.

Categorized in:

Python,

Last Update: March 15, 2024