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,775
  • 3
  • 25
  • 53
Rares Dima
  • 1,575
  • 1
  • 15
  • 38
  • 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
  • 33,322
  • 10
  • 107
  • 191
2

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

TL;DR use little r

myString = r'\n'
cs95
  • 379,657
  • 97
  • 704
  • 746
Blue Granny
  • 772
  • 1
  • 8
  • 24