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?
It is a one-dimensional array of characters. One or more characters enclosed in a pair of matching single or double quotes can be considered a string in R.
The f-strings have the f prefix and use {} brackets to evaluate values. Format specifiers for types, padding, or aligning are specified after the colon character; for instance: f'{price:. 3}' , where price is a variable name.
r means the string will be treated as raw string. See the official Python 2 Reference about "String literals": When an 'r' or 'R' prefix is present, a character following a backslash is included in the string without change, and all backslashes are left in the string.
The r prefix on strings stands for “raw strings”. Standard strings use backslash for escape characters: “\n” is a newline, not backslash-n.
"\n"
is a string containing a newline character, and r"\n"
is a string containing a backslash and the letter n
.\a
bit working correctly without double escapes. Note that '$\\alpha > \\beta$'
is identical to r'$\alpha > \beta$'
.r, u, f, rf, fr, b, rb, br
bytes
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'
is False
. These are not the same thing.u
prefix is allowed for backward compatibility with Python 2. In Python 2, strings are ASCII by default, and the u
prefix allows you to include non-ASCII characters in your strings. For example, note the accented character in the french phrase u"Fichier non trouvé"
.r, u, f, b
. Even b
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 the answer instead of buried in a comment, @JamesGibson 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With