- An
r
-string is a raw string.
- It ignores escape characters. For example,
"\n"
is a string containing a newline character, andr"\n"
is a string containing a backslash and the lettern
. - If you wanted to compare it to an
f
-string, you could think off
-strings as being “batteries-included.” They have tons of flexibility in the ability to escape characters and execute nearly arbitrary expressions. Ther
-string on the other hand is stripped down and minimalist, containing precisely the characters between its quotation marks. - As far as actually using the things, typically you would use an
r
-string if you’re passing the string into something else that uses a bunch of weird characters or does its own escaping so that you don’t have to think too hard about how many backslashes you really need to get everything to work correctly. In your example, they at least neededr
-strings to get the\a
bit working correctly without double escapes. Note that'$\\alpha > \\beta$'
is identical tor'$\alpha > \beta$'
.
- Since you’re using
f
-strings, I’ll assume you have at least Python 3.6. Not all of these options are supported for older versions but any of the following prefixes are valid in Python 3.6+ in any combination of caps and lowers:r, u, f, rf, fr, b, rb, br
- The
b
-strings are binary literals. In Python 2 they do nothing and only exist so that the source code is compatible with Python 3. In Python 3, they allow you to create abytes
object. Strings can be thought of as a view of the underlying bytes, often restricted as to which combinations are allowed. The distinction in types helps to prevent errors from blindly applying text techniques to raw data. In Python 3, note that'A'==b'A'
isFalse
. These are not the same thing. - The
u
-strings are unicode literals. Strings are unicode by default in Python 3, but theu
prefix is allowed for backward compatibility with Python 2. In Python 2, strings are ASCII by default, and theu
prefix allows you to include non-ASCII characters in your strings. For example, note the accented character in the French phraseu"Fichier non trouvé"
. - In the kind of code I write, I rarely need anything beyond
r
,u
,f
, andb
. Evenb
is a bit out there. Other people deal with those prefixes every day (presumably). They aren’t necessarily anything you need to familiarize yourself with, but knowing they exist and being able to find their documentation is probably a good skill to have.
Just so that it’s in an answer instead of buried in a comment, Peter Gibson linked the language specification, and that’s the same place I pulled the prefix list from. With your math background, a formal language specification might be especially interesting — depending a little on how much you like algebra and mathematical logic.
Even if it’s just for a semantically trivial language like Forth, I think many programmers would enjoy writing a short interpreter and gain valuable insight into how their language of choice works.