2

As we all know,we can create a string like this:

str1 = r"\abc\test"

But if I want put the \ in the end of a string like:

str2 = r"\abc\test\"

A syntax error occurs ! I have found an answer,but it's in JavaScript. `String.raw` when last character is `\` So,How to deal this in python

Ben-xue
  • 103
  • 1
  • 6

2 Answers2

3

You can concat another normal string:

>>> r'\abc\test' + '\\'
'\\abc\\test\\'
Uriel
  • 15,579
  • 6
  • 25
  • 46
0

duplicated Why can't Python's raw string literals end with a single backslash?

https://docs.python.org/3/reference/lexical_analysis.html

Specifically, a raw literal cannot end in a single backslash (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the literal, not as a line continuation.

Eduard Gamonal
  • 8,023
  • 5
  • 41
  • 46