-2

Bear with me I am new to Python. I wrote a simple script to parse a long string which contains multiple substrings wrapped in single quote. I used str.replace to replace single quote to empty string. However input string is changed with double quote instead. I ran the script with new input by accident and in my surprise it still replaced all double quote to empty string.

I thought I read in somewhere in Python, double quote and single quote are treated same. Is that correct statement?

Update:

My apology. I must have confused others with this question including myself. I ran quick test script to verify the output what I saw from the original script however I don't see the same output. I tried to replace input string contains double quote (e.g. str="foo \"abc\" \"efg\"" with str.replace("'","") but it didn't do. I had to re-examine the original script and input string it is trying to parse. Thank you for taking your time looking into this thread.

I just verified that part of the original script (rather long and a bit complex one by saying that I didn't tell the whole truth about this script in the context of my question, I am sorry about that) misled me to believe (for a while) Python str.replace treat double and single quote in same manner which was wrong assumption.

DaeYoung
  • 1,161
  • 6
  • 27
  • 59
  • 2
    Possible duplicate of [Single quotes vs. double quotes in Python](https://stackoverflow.com/questions/56011/single-quotes-vs-double-quotes-in-python) – swhat Aug 09 '18 at 17:52
  • 2
    I'm not following your description of events. Can you post some code and its output? – John Kugelman Aug 09 '18 at 17:52
  • reference pep8 https://www.python.org/dev/peps/pep-0008/#string-quotes – RJ7 Aug 09 '18 at 17:55

2 Answers2

2

In python, they are treated the same. But that means, as far as the language is concerned, you can use either single or double quotes. However, they are not the same character. If you have eg.

text = "I said 'hello' to him"

the outer, double quotes just delimit the string, while the inner, single quotes are part of the string itself. So, if you run

text.replace("'", "")

they single quotes will be replaced. But if you do

text.replace('"', "")

nothing happens, since your string does not contain double quotes

blue_note
  • 27,712
  • 9
  • 72
  • 90
1

Single- and Double-quotes are treated the same in your code. In other words,

'Hello' == "Hello"

and whether you use ' or " makes no difference.

Say, however, that you have a string that contains the characters ' or ":

long_string = "So I said 'and what do you mean by that?' and she responded 'I dunno' and ..."

This is a string that contains a ' character. Note that I used " as the character to designate this string in the code; therefore, I can use ' characters inside the string without having to escape them. It works the same the other way around:

>>> print( '"' )   # "
>>> print( "'" )   # '

If you want a double-quote character inside a double-quoted string, then you have to escape it:

>>> print( "\"" )  # "
>>> print( '\'' )  # '

Now, back to your question. You had a string like long_string, with single-quote characters inside it. To remove them, you can simply do

string_without_quotes = long_string.replace( "'", "" ) # or '\''
Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53