1

I am working with regex and wanted to save the following to a string. Something like: r_string=':"\<\d>"'

r_string=':"\<\d\>"'

however, when I print out the string, I get a double \

Even using the following results in double :

r_string=':"\\<\\d\\>"'

any ideas why ?

Number Logic
  • 732
  • 1
  • 7
  • 17
  • 2
    This is likely just the presentation you see to tell you that "this is not actually a `\ – lucidbrot May 24 '20 at 20:49
  • 2
    I don't actually get double when printing, but you probably want to make that string raw: by adding an `r` prefix: `r':"\"'` – Tomerikoo May 24 '20 at 20:49
  • 1
    You will see double backslashes when using default IDLE output, but not when using `print` or writing to a text file – Luka Mesaric May 24 '20 at 20:51
  • possibly relevant: [how to escape special characters of a string with single backslashes](https://stackoverflow.com/a/37049166/2550406) – lucidbrot May 24 '20 at 20:52
  • 1
    TL;DR: Console output (inspection) is calling the `__repr__` method which shows the full string, backslashes escaped. printing on the other hand, calls the `__str__` method which prints clean, without escapes – Tomerikoo May 24 '20 at 20:55

0 Answers0