The escape symbol of'(Single quote) may be displayed as it is in Python's interactive mode.

If you enter the following in python interactive mode, I heard that the \ before the'is displayed.

>>> "\"No, I can\'t\" He said."
# '"No, I can\'t" He said.'Is displayed
>>> print("\"No, I can\'t\" He said.")
# "No, I can't" He said.No escape symbol

If you use it as an argument of print (), it will appear as intended.

This behavior is by design. A similar case is introduced in the official Doc. It's embarrassing ... I will consider why it is the specification in the future based on the comments I received.

~~ Let's write a prediction about it. (I may find the basis soon ~~

environment

https://www.python.org/shell/

Input values and their results

"abc" →'abc' """" →'""' ""'" →'"'' "''" → "''" "a'" → "a'"

What you can see from the above

---'Will be preceded by \ when "and' are mixed --The character string is output by enclosing it in', but if it contains', enclose it in "(this is a specification).

The following is a consideration of slipping, so I erased it. Really embarrassing ...

Possibly

~~ Isn't it like this?

  1. Enter "" '" (I want to display"')
  2. Interpreting up to [1] produces the string'"'
  3. If you interpret up to [3], you can see that'is included in the character string.
  4. Generate a new character string while keeping the character string of 2. Since the enclosing character is ". Since the enclosing character is", \ does not function as an escape character [^ 1]. Therefore, you should remove , but you inadvertently create a __ string including __.
  5. The character string "" and "'" are created, so combine them and output![^ 2] ~~

from now on

As I got in the comment, I would like to investigate the difference between str () and repr () and think about why it settled down to the current output. However, if you know Python, it may be an immediate answer level, so I may delete the article.

[^ 1]: In Bash, it seems that \ is not escaped when you enclose a string with ""

[^ 2]: By the way, if you enter'\ "'+" '", it will be output as'"''. If it is "'" alone, "'" will be displayed, so it may be suspicious that the combination is strange.

Recommended Posts

The escape symbol of'(Single quote) may be displayed as it is in Python's interactive mode.
The date is displayed incorrectly in matplotlib.
PyQtGraph may not be available in the interpreter.