1

In F# there is something called a literal string (not string literal), basically if a string literal is preceded by @ then it is interpreted as-is, without any escapes.

For example if you want to write the path of a file in Windows(for an os.walk for example) you would do it like this:

"d:\\projects\\re\\p1\\v1\\pjName\\log\\"

Or you could do this(the F# way):

@"d:\projects\re\p1\v1\pjName\log\"

The second variant looks much more clear and pleasing to the eye. Is there something of the sort in python? The documentation doesn't seem to have anything regarding that.

I am working in Python 3.6.3.

Kunal Mukherjee
  • 5,463
  • 3
  • 23
  • 47
Rares Dima
  • 1,502
  • 12
  • 35
  • 1
    Have you tried raw or unicode strings like `path = r'"d:\projects\re\p1\v1\pjName\log\"` – Kunal Mukherjee Jan 15 '18 at 07:14
  • 1
    @KunalMukherjee Note that a string literal cannot end with a single backslash, even if you use raw strings. One way to work around that is to use literal string concatenation, eg `path = r'd:\projects\re\p1\v1\pjName\log' '\\'` – PM 2Ring Jan 15 '18 at 07:19

2 Answers2

4

There are: https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals

You can use r prefix.

viraptor
  • 32,414
  • 8
  • 104
  • 182
2

https://docs.python.org/2.0/ref/strings.html

TL;DR use little r

myString = r'\n'
cs95
  • 330,695
  • 80
  • 606
  • 657
Blue Granny
  • 752
  • 5
  • 23