pythonstring

how to create a raw string when its last character is a `\` in python


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, Hhw to deal with this in python?


Solution

  • You can concat another normal string:

    >>> r'\abc\test' + '\\'
    '\\abc\\test\\'