6

For instance, I write a normal string and another "abnormal" string like this:

enter image description here

Now I debug it, finding that in the debug tool, the "abnormal" string will be shown like this:

enter image description here

Here's the question:

Why does PyCharm show double backslashes instead of a single backslash? As is known to all, \' means '. Is there any trick?

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
guo
  • 9,674
  • 9
  • 41
  • 79
  • If your string contains an actual backslash, does PyCharm show `\\\\ ` instead of `\\ `? – ruakh Apr 03 '16 at 15:18
  • @ruakh In practice,`string='abc\\abc' ` then`print string` will show `abc\abc` in console, but `abc\\abc` in PyCharm debugger-Variables. – guo Apr 04 '16 at 01:59
  • It doesn't know that you are writing `\'`, you may write `\n`, so to make sure the display isn't broken, it is double escaping any special characters. – Burhan Khalid Apr 05 '16 at 06:29
  • What is `make sure the display isn't broken` ? @BurhanKhalid – guo Apr 05 '16 at 06:45
  • Does this answer your question? [Can '\' be in a Python string?](https://stackoverflow.com/questions/22537199/can-be-in-a-python-string) – Gert Arnold Dec 11 '22 at 10:04

1 Answers1

-1

What I believe is happening is the ' in your c variable string needs to be escaped and PyCharm knows this at runtime, given you have surrounded the full string in " (You'll notice in the debugger, your c string is now surrounded by '). To escape the single quote it changes it to \', but now, there is a \ in your string that needs escaping, and to escape \ in Python, you type \\.

EDIT Let me see if I can explain the order of escaping going on here.

  1. "u' this is not normal" is assigned to c
  2. PyCharm converts the string in c to 'u' this is not normal' at runtime. See how, without escaping the 2nd ', your string is now closed off right after u.
  3. PyCharm escapes the ' automatically for you by adding a slash before it. The string is now 'u\' this is not normal'. At this point, everything should be fine but PyCharm may be taking an additional step for safety.
  4. PyCharm then escapes the slash it just added to your string, leaving the string as: 'u\\' this is not normal'.

It is likely a setting inside PyCharm. Does it cause an actual issue with your code?

Kyle Blake
  • 47
  • 2
  • 4
  • I am still confused. What's the role that the left side ` \ ` play? i.e, what do you mean by saying ` to escape \ in Python` ? I also don't understand the escape order. – guo Apr 03 '16 at 14:22
  • 1
    Yes, the thing is, the escaping is completely redundant, because there's no need to escape a single-quote inside a double-quoted string. For some reason, pycharm seems to want to display all strings in single-quotes, which has the ugly side-effect of forcing escaping of embedded single-quotes. It should learn from python, and simply display the string like this: `"u' this is not normal"` (i.e. don't try to fix something which isn't broken). – ekhumoro Apr 05 '16 at 17:27