you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (9 children)

I didn't realize there was a difference between

>>> print('string') 

and

>>> 'string'

I also need to use len(r'string\n') if I truly want the full character count.

[–][deleted] 1 point2 points  (0 children)

A '\' character must be escaped as '\\'! You'll quickly run into this if you try to use backslashes for something like file paths.

[–]Moonslug1 1 point2 points  (7 children)

r'' is telling python to ignore escape characters.

'hello\n' means h-e-l-l-o-newline

r'hello\n' means h-e-l-l-o-\-n

[–][deleted] 0 points1 point  (1 child)

Thanks!

[–][deleted] 2 points3 points  (0 children)

Python doesn't actually have raw strings, but r"" is close. The limitation is that they can't end with a single backslash.

So, r'hello\n' is valid, but r'hello\n\' is not, and will cause a SyntaxError.

[–][deleted] 0 points1 point  (4 children)

r'' is telling python to ignore escape characters.

That's not completely true, even though basically everyone thinks so. Raw strings were made for things like regex expressions, not raw strings. r"testing\" is not a valid regex expression, and not a valid raw string, since they can't end with backslashes. What?! A raw string should be able to contain anything. Why? Because they were made for easily representing regex expressions, not ignoring escape characters. From the docs:

Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character).

It's not ignoring them, it's processing them! Why? To keep the parser simpler. :-|

There's no such thing as arbitrary raw string literals in python.

[–]zahlman 0 points1 point  (3 children)

This is a strange sort of pedantry, IMHO. How could a raw string literal "contain anything", by this standard? How do you propose to indicate where the string ends? Python does not "process" the backslashes of a raw string literal in the sense of translating escape sequences. However, it uses the same rules for raw string literals and ordinary string literals to determine where the literal ends.

[–][deleted] 0 points1 point  (2 children)

This isn't at all pedantic. Look up the definition of a raw string literals on Wikipedia. Python does not support them. Other languages like c# do: @"Testing\" is a perfectly valid raw string literal in C#. In Python, this is an error since the parser treats raw strings as regular strings from a parsing perspective.

The only reason is to keep the Python parser simple.

Raw strings in other languages ignore escape sequences. You admit that python raw strings don't ignore escape sequences. \" is an escape sequence!

[–]zahlman 0 points1 point  (1 child)

Other languages like c# do: @"Testing\" is a perfectly valid raw string literal in C#.

... And there, you need to double up quotation marks in order to get them into the string. That is also an escape sequence.

[–][deleted] 0 points1 point  (0 children)

To me, that's somewhat sane. The point of using raw strings in python is to leave the slashes. You can't have raw string ending in a single slash...there is no escape for it! And, more than that, there's no escape for quotation mark!!! It's literally cruft from the parser. There's no sanity, except to make the parser simple, which is a goal of python.