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
  • 852
  • 1
  • 9
  • 19
  • 2
    This is likely just the presentation you see to tell you that "this is not actually a `\<` (escaped less than) but it's a backslash and then a less than". I.e. it works as you think it does, but printing it looks differently.Or if it is not doing what you want, prefix the string with `r` as explained e.g. [here](https://kite.com/python/answers/how-to-print-a-backslash-in-python) – 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':"\<\d\>"'` – 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