'I know of f-strings, but what are r-strings? Are there others?
I started learning python for the first time in an accelerated course on data science a few weeks ago and we were introduced early on to f-strings.
The simple code:
name = 'Tim'
print(f'There are some who call me {name}...')
outputs the string "There are some who call me Tim..."
Through my browsing of various packages out of curiosity, I came upon pages like this one detailing a function you can call in matplotlib to render $\LaTeX$-like expressions within the generated images. In the example code they use something similar to f-strings but with an r instead of an f.
import matplotlib.pyplot as plt
plt.title(r'$\alpha > \beta$')
plt.show()
The resulting (otherwise empty) graph has a title using text which has been formatted similarly to how one would expect using MathJax or $\LaTeX$ with a greek character alpha and a greek character beta.
My questions are the following:
What precisely is an r-string and how does it compare to an f-string? Are r-strings specifically used for matplotlib's mathtext and usetex?
Apart from f-strings and r-strings, are there any other notable similar string variants or alternates that I should familiarize myself with or be made aware of?
Solution 1:[1]
- 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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Community |