Search code examples
pythonstrip

Using strip() to removed double quotes


Need help to strip double quotes from the variable a

>>> a
' "AZ-EDH"'

when following command is executed then only last double quotes are stripped

>>> a.strip('"')
' "AZ-EDH'

whereas when any of the following command is executed then both spaces & double quotes are stripped.

>>> a.strip('" ')
'AZ-EDH'

>>> a.strip(' " ')
'AZ-EDH'

>>> a.strip(' "')
'AZ-EDH'

What am I missing here?


Solution

  • Python str.strip() only removes leading and trailing characters.

    The first " in ' "AZ-EDH"' is not a leading character because of the space.

    The optional parameter is a set of characters to remove. This means your examples of ' "', '" ', ' " ' are identical in meaning. They remove all leading " and .

    https://python-reference.readthedocs.io/en/latest/docs/str/strip.html

    You might be looking for str.replace('"', '').

    https://python-reference.readthedocs.io/en/latest/docs/str/replace.html