Remove quotes from String in Python Remove quotes from String in Python python python

Remove quotes from String in Python


Just use string methods .replace() if they occur throughout, or .strip() if they only occur at the start and/or finish:

a = '"sajdkasjdsak" "asdasdasds"' a = a.replace('"', '')'sajdkasjdsak asdasdasds'# or, if they only occur at start and end...a = a.strip('\"')'sajdkasjdsak" "asdasdasds'# or, if they only occur at start...a = a.lstrip('\"')# or, if they only occur at end...a = a.rstrip('\"')


You can use eval() for this purpose

>>> url = "'http address'">>> eval(url)'http address'

while eval() poses risk , i think in this context it is safe.


There are several ways this can be accomplished.

  • You can make use of the builtin string function .replace() to replace all occurrences of quotes in a given string:

    >>> s = '"abcd" efgh'>>> s.replace('"', '')'abcd efgh'>>> 
  • You can use the string function .join() and a generator expression to remove all quotes from a given string:

    >>> s = '"abcd" efgh'>>> ''.join(c for c in s if c not in '"')'abcd efgh'>>> 
  • You can use a regular expression to remove all quotes from given string. This has the added advantage of letting you have control over when and where a quote should be deleted:

    >>> s = '"abcd" efgh'>>> import re>>> re.sub('"', '', s)'abcd efgh'>>>