Asked  7 Months ago    Answers:  5   Viewed   42 times

Technically, any odd number of backslashes, as described in the documentation.

>>> r''
  File "<stdin>", line 1
SyntaxError: EOL while scanning string literal
>>> r'\'
>>> r'\'
  File "<stdin>", line 1
SyntaxError: EOL while scanning string literal

It seems like the parser could just treat backslashes in raw strings as regular characters (isn't that what raw strings are all about?), but I'm probably missing something obvious.



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.

Tuesday, June 1, 2021
answered 7 Months ago

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 '.


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.

Wednesday, June 9, 2021
answered 6 Months ago

What you're talking about (I think) are called docstrings (Thanks Boud for the link).

def foo():
    """This function does absolutely nothing"""

Now, if you type help(foo) from the interpreter, you'll get to see the string that I put in the function. You can also access that string by foo.__doc__

Of course, string literals are just that -- literal strings.

a = "This is a string literal"  #the string on the right side is a string literal, "a" is a string variable.


foo("I'm passing this string literal to a function")

They can be defined in a bunch of ways:

'single quotes'
"double quotes"
""" triple-double quotes """  #This can contain line breaks!

or even

#This can contain line breaks too!  See?
''' triple-single 
    quotes '''
Monday, June 28, 2021
answered 6 Months ago

This should do it: "C:\backup\".replace(/\\/g, '\')

In the regular expression, a single must be escaped to \, and in the replacement also.

[edit 2021] Maybe it's better to use template literals.

console.log(`original solution ${"C:\backup\".replace(/\\/g, '\')}`)

// a template literal will automagically replace \ with 
console.log(`template string without further ado ${`C:\backup\`}`);

// but if they are escaped themselves
console.log(`Double escaped ${`C:\\backup\\`.replace(/\\/g, '\')}`);

// don't want to replace the second \
console.log(`not the second ${`C:\\backup\\`.replace(/\\/, '\')}`);

// don't want to replace the first \
console.log(`not the first ${`C:\\backup\`.replace(/[\]$/, '\')}`);
Friday, August 13, 2021
answered 4 Months ago

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('\')
>>> r'\' # Two literal backslashes, 2 is even so this is doable with raw.
>>> len(r'\')
>>> '\'*3 # Three literal backslashes, only doable with ordinary literals.
>>> len('\'*3)

This answer is only meant to complement the other one.

Monday, August 23, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :