Why can't Python's raw string literals end with a single backslash?
The reason is explained in the part of that section which I highlighted in bold:
String quotes can be escaped with a
backslash, but the backslash remains
in the string; for example,r"\""
is a
valid string literal consisting of two
characters: a backslash and a double
quote;r"\"
is not a valid string
literal (even a raw string cannot end
in an odd number of backslashes).
Specifically, a raw string 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 string, not as a line
continuation.
So raw strings are not 100% raw, there is still some rudimentary backslash-processing.
how to create a raw string when it's last character is a `\` in python
You can concat another normal string:
>>> r'\abc\test' + '\\'
'\\abc\\test\\'
Why can't I end a raw string with a backslash?
You still need \
to escape '
or "
in raw strings, since otherwise the python interpreter doesn't know where the string stops. In your example, you're escaping the closing '
.
Otherwise:
r'it wouldn\'t be possible to store this string'
r'since it'd produce a syntax error without the escape'
Look at the syntax highlighting to see what I mean.
Python: Why do raw strings require backslash to be escaped?
because '\'
has special meaning in re it means escape the character after it in the language you use to define a re so if you want to match '+'
as a character your re will be '\+'
Python - Raw String Literals
The only way to put in a single quote into a string started with a single quote is to escape it. Thus, both raw and regular string literals will allow escaping of quote characters when you have an unescaped backslash followed by a quote character. Because of the requirement that there must be a way to express single (or double) quotes inside string literals that begin with single (or double) quotes, the string literal '\'
is not legal, whether you use a raw or regular string literal.
To get any arbitrary string with an odd number of literal backslashes, I believe the best way is to use regular string literals. This is because trying to use r'\\'
will work, but it will give you a string with two backslashes instead of one:
>>> '\\' # A single literal backslash.
'\\'
>>> len('\\')
1
>>> r'\\' # Two literal backslashes, 2 is even so this is doable with raw.
'\\\\'
>>> len(r'\\')
2
>>> '\\'*3 # Three literal backslashes, only doable with ordinary literals.
'\\\\\\'
>>> len('\\'*3)
3
This answer is only meant to complement the other one.
Python: Trailing backslash in raw strings
From the documentation,
Specifically, a raw string 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 string, not as a line continuation.
Python raw strings and trailing backslash
It's a FAQ.
And in response to "you really want your string to end with a backslash. There's no way to do that in a 'raw' string.": the FAQ shows how to workaround it.
>>> r'ab\c' '\\' == 'ab\\c\\'
True
>>>
Related Topics
How Do Python'S Any and All Functions Work
Can't Modify List Elements in a Loop
How to Get Time of a Python Program'S Execution
Using Numpy to Build an Array of All Combinations of Two Arrays
How to Wait Some Time in Pygame
Unicodedecodeerror When Reading CSV File in Pandas With Python
Changing Default Encoding of Python
What Is the Formal Difference Between "Print" and "Return"
How to Prevent Tensorflow from Allocating the Totality of a Gpu Memory
Adding Python to Path on Windows
Why Does the Expression 0 ≪ 0 == 0 Return False in Python
How to Sort a Dataframe in Python Pandas by Two or More Columns
Selecting Multiple Columns in a Pandas Dataframe
How to Improve Performance of This Code
"Unicode Error "Unicodeescape" Codec Can't Decode Bytes... Cannot Open Text Files in Python 3