• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Using Python to Remove Quotes from String

Using Python to Remove Quotes from String

February 15, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  How to Check if a Letter is in a String Using Python
  • 2.  Euclidean Algorithm and Extended Euclidean Algorithm in Python
  • 3.  Get Python Dictionary Values as List
  • 4.  islower Python – Check if All Letters in String Are Lowercase
  • 5.  Python Increment Counter with += Increment Operator
  • 6.  Get Month Name from Date in Python
  • 7.  pandas percentile – Calculate Percentiles of Series or Columns in DataFrame
  • 8.  Replace Values in Dictionary in Python
  • 9.  pandas drop – Drop Rows or Columns from DataFrame
  • 10.  How to Sort Numbers in Python Without Sort Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy